From f48584a621e950ccf739099fe93e0516f81648a4 Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Sun, 29 Dec 2024 17:59:53 +0900 Subject: [PATCH 01/36] composition of subprob kernels --- theories/kernel.v | 637 ++++++++++++++++++++++++++++++++++++++++++- theories/measure.v | 2 +- theories/numfun.v | 22 ++ theories/sequences.v | 31 ++- 4 files changed, 681 insertions(+), 11 deletions(-) diff --git a/theories/kernel.v b/theories/kernel.v index 31d8ba818..7983a18c5 100644 --- a/theories/kernel.v +++ b/theories/kernel.v @@ -1,6 +1,7 @@ (* mathcomp analysis (c) 2022 Inria and AIST. License: CeCILL-C. *) From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval finmap. +From mathcomp Require Import archimedean. From mathcomp Require Import mathcomp_extra boolp classical_sets functions. From mathcomp Require Import cardinality fsbigop reals ereal signed. From mathcomp Require Import topology normedtype sequences esum measure. @@ -59,6 +60,8 @@ Reserved Notation "R .-sfker X ~> Y" (at level 42, format "R .-sfker X ~> Y"). Reserved Notation "R .-fker X ~> Y" (at level 42, format "R .-fker X ~> Y"). +Reserved Notation "R .-ftker X ~> Y" + (at level 42, format "R .-ftker X ~> Y"). Reserved Notation "R .-spker X ~> Y" (at level 42, format "R .-spker X ~> Y"). Reserved Notation "R .-pker X ~> Y" @@ -187,7 +190,21 @@ have ? : m1 = m2. by subst m1; f_equal; f_equal; f_equal; apply/Prop_irrelevance. Qed. -HB.mixin Record SFiniteKernel_isFinite d d' +(* interface for finite transition kernel *) +HB.mixin Record SFiniteKernel_isFiniteTransition + d d' (X : measurableType d) (Y : measurableType d') + (R : realType) (k : X -> {measure set Y -> \bar R}) := { + finite_finite_transition_kernel : forall x, fin_num_fun (k x) }. + +#[short(type=finite_transition_kernel)] +HB.structure Definition FiniteTransitionKernel + d d' (X : measurableType d) (Y : measurableType d') (R : realType) := + { k of @SFiniteKernel _ _ _ _ _ k & + SFiniteKernel_isFiniteTransition _ _ X Y R k }. + +Notation "R .-ftker X ~> Y" := (finite_transition_kernel X%type Y R). + +HB.mixin Record FiniteTransitionKernel_isFinite d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : X -> {measure set Y -> \bar R}) := { measure_uub : measure_fam_uub k }. @@ -195,8 +212,8 @@ HB.mixin Record SFiniteKernel_isFinite d d' #[short(type=finite_kernel)] HB.structure Definition FiniteKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) := - { k of @SFiniteKernel _ _ _ _ _ k & - SFiniteKernel_isFinite _ _ X Y R k }. + { k of @FiniteTransitionKernel _ _ _ _ _ k & + FiniteTransitionKernel_isFinite _ _ X Y R k }. Notation "R .-fker X ~> Y" := (finite_kernel X%type Y R). Arguments measure_uub {_ _ _ _ _} _. @@ -240,8 +257,20 @@ Qed. HB.instance Definition _ := @Kernel_isSFinite_subdef.Build d d' X Y R k sfinite_finite. +Let finite_transition_finite : forall x, fin_num_fun (k x). +Proof. +move=> x U mU. +have [r kr] := measure_uub. +rewrite ge0_fin_numE//. +rewrite (@le_lt_trans _ _ (k x setT)) ?le_measure ?inE//. +by rewrite (lt_trans (kr x)) ?ltry. +Qed. + +HB.instance Definition _ := + @SFiniteKernel_isFiniteTransition.Build d d' X Y R k finite_transition_finite. + HB.instance Definition _ := - @SFiniteKernel_isFinite.Build d d' X Y R k measure_uub. + @FiniteTransitionKernel_isFinite.Build d d' X Y R k measure_uub. HB.end. @@ -350,6 +379,15 @@ HB.structure Definition SubProbabilityKernel FiniteKernel_isSubProbability _ _ X Y R k }. Notation "R .-spker X ~> Y" := (sprobability_kernel X%type Y R). +Lemma sprob_kernelP d d' (X : measurableType d) (Y : measurableType d') + (R : realType) (k : X -> set Y -> \bar R) : + (ereal_sup [set k x [set: _] | x in [set: _]] <= 1)%E <-> + forall x, (k x setT <= 1)%E. +Proof. +split => [+ x|k1]; last by apply: ub_ereal_sup => _ /= [z _ <-]; exact: k1. +by apply/le_trans/ereal_sup_ubound => /=; exists x. +Qed. + HB.factory Record Kernel_isSubProbability d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : X -> {measure set Y -> \bar R}) of isKernel _ _ X Y R k := { @@ -764,7 +802,6 @@ HB.instance Definition _ (P : probability Y R):= End knormalize. -(* TODO: useful? *) Lemma measurable_fun_mnormalize d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : R.-ker X ~> Y) : measurable_fun [set: X] (fun x => mnormalize (k x) point : pprobability Y R). @@ -1099,3 +1136,593 @@ by apply: eq_integral => z _; apply/cvg_lim => //; exact: cvg_nnsfun_approx. Qed. End integral_kcomp. + +(* composition of finite transition kernels, following Klenke *) + +(* lem 14.13 *) +Section measurable_fun_prod. +Context d1 d2 d3 (T1 : measurableType d1) (T2 : measurableType d2) + (T3 : measurableType d3). + +Lemma measurable_prod1 (f : T1 * T2 -> T3) (y : T2) : + measurable_fun setT f -> measurable_fun setT (fun x => f (x, y)). +Proof. by move=> mf; exact: measurableT_comp. Qed. + +Lemma measurable_prod2 (f : T1 * T2 -> T3) (x : T1) : + measurable_fun setT f -> measurable_fun setT (fun y => f (x, y)). +Proof. by move=> mf; exact: measurableT_comp. Qed. + +End measurable_fun_prod. + +HB.mixin Record isSigmaFiniteTransitionKernel + d d' (X : measurableType d) (Y : measurableType d') + (R : realType) (k : X -> {measure set Y -> \bar R}) := { + sigma_finite_finite_transition_kernel : forall x, sigma_finite setT (k x) }. + +#[short(type=sigma_finite_transition_kernel)] +HB.structure Definition SigmaFiniteTransitionKernel + d d' (X : measurableType d) (Y : measurableType d') (R : realType) := + { k of @Kernel _ _ _ _ _ k & isSigmaFiniteTransitionKernel _ _ X Y R k }. + +Reserved Notation "R .-sftker X ~> Y" + (at level 42, format "R .-sftker X ~> Y"). +Notation "R .-sftker X ~> Y" := (sigma_finite_transition_kernel X%type Y R). + +Lemma sprob_kernel_le1 d d' (X : measurableType d) + (Y : measurableType d') (R : realType) (k : R.-spker X ~> Y) x : + k x [set: Y] <= 1. +Proof. +by apply: (sprob_kernelP (fun x A => k x A)).1; exact: sprob_kernel. +Qed. + +(* composition of a finite transition kernel and a function, as in def 14.20 *) +Definition kfcomp d1 d2 + (T1 : measurableType d1) (T2 : measurableType d2) (R : realType) + (k : R.-ftker T1 ~> T2) (f : T1 * T2 -> \bar R) : T1 -> \bar R := + fun x => \int[k x]_y f (x, y). + +Section measurable_kfcomp. + +Let finite_measure_sigma_finite d (T : measurableType d) + (R : realType) (mu : {measure set T -> \bar R}) : + fin_num_fun mu -> sigma_finite setT mu. +Proof. +by move=> fmu; apply: fin_num_fun_sigma_finite => //; rewrite measure0. +Qed. + +Let finite_measure_sfinite d (T : measurableType d) + (R : realType) (mu : {measure set T -> \bar R}) : + fin_num_fun mu -> sfinite_measure mu. +Proof. +move=> fmu. +exists (fun n => if n is O then mu else mzero) => [[]//|U mU]. +by rewrite /mseries nneseries_recl// eseries0 ?adde0// => -[|]. +Qed. + +Import HBNNSimple. + +(* lem 14.20 *) +Lemma measurable_kfcomp d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) + (R : realType) (k : R.-ftker T1 ~> T2) + (f : T1 * T2 -> \bar R) : measurable_fun [set: (T1 * T2)%type] f -> + (forall t, (0 <= f t)%E) -> + measurable_fun [set: T1] (kfcomp k f). +Proof. +move=> mf f0. +rewrite /kfcomp. +pose I (f : T1 * T2 -> \bar R) x := \int[k x]_y f (x, y). +pose g (A1 : set T1) (A2 : set T2) : T1 * T2 -> \bar R := + EFin \o \1_(A1 `*` A2). +have IgE (A1 : set T1) (A2 : set T2) x : measurable A1 -> measurable A2 -> + I (g A1 A2) x = (\1_A1 x)%:E * k x A2. + move=> mA1 mA2. + rewrite /I /g/= integral_indic//; last first. + rewrite [X in measurable X](_ : _ = xsection (A1 `*` A2) x); last first. + by rewrite xsectionE. + by apply: measurable_xsection; exact: measurableX. + have [xA1|xA1] := boolP (x \in A1). + rewrite indicE xA1 mul1e; congr (k _ _). + by rewrite setIT/=; apply/seteqP; split => [y []//|y]; move/set_mem: xA1. + rewrite indicE (negbTE xA1) mul0e [X in k _ X = _](_ : _ = set0)//. + by rewrite setIT -subset0 => y -[/= /mem_set]; rewrite (negbTE xA1). +suff: measurable_fun [set: T1] (I f) by []. +pose f_ := nnsfun_approx measurableT mf. +have If_cvg x : I (EFin \o f_ n) x @[n --> \oo] --> I f x. + rewrite /I. + pose g' := fun n y => (EFin \o f_ n) (x, y). + rewrite [X in _ --> X](_ : _ = + \int[k x]_y (fun t => limn (g' ^~ t)) y); last first. + apply: eq_integral => y _. + by apply/esym/cvg_lim => //; exact: cvg_nnsfun_approx. + apply: cvg_monotone_convergence => //. + - by move=> n; apply/measurable_EFinP; exact: measurableT_comp. + - by move=> n y _; rewrite /= lee_fin. + - move=> y _ a b ab /=; rewrite lee_fin. + by have /lefP/(_ (x, y)) := nd_nnsfun_approx measurableT mf ab. +apply: (emeasurable_fun_cvg (fun n => I (EFin \o f_ n))). + move=> m. + pose D := [set A | measurable A /\ measurable_fun setT (I (EFin \o \1_A))]. + have setSD_D : setSD_closed D. + move=> B A AB; rewrite /D/= => -[mB mIB] [mA mIA]. + have IE : I (EFin \o \1_(B `\` A)) = I (EFin \o \1_B) \- I (EFin \o \1_A). + apply/funext => x/=. + rewrite /I. + pose kx : {measure set T2 -> \bar R} := k x. + pose H1 := isFinite.Build _ _ _ _ + (@finite_finite_transition_kernel _ _ _ _ R k x). + pose H2 := isSFinite.Build _ _ _ _ (finite_measure_sfinite + (@finite_finite_transition_kernel _ _ _ _ R k x)). + pose H3 := isSigmaFinite.Build _ _ _ _ (finite_measure_sigma_finite + (@finite_finite_transition_kernel _ _ _ _ R k x)). + pose kx' := HB.pack_for (FiniteMeasure.type T2 R) (Measure.sort kx) H1 H2 H3. + have kxE : kx = kx' by exact: eq_measure. + rewrite (*TODO: use RintegralB when merged*) -integralB_EFin//; last 2 first. + - rewrite -/kx kxE; apply: integrable_indic => //. + rewrite -[X in measurable X]/(_ @^-1` _) -xsectionE. + exact: measurable_xsection. + - rewrite -/kx kxE; apply: integrable_indic => //. + rewrite -[X in measurable X]/(_ @^-1` _) -xsectionE. + exact: measurable_xsection. + apply: eq_integral => y _/=. + rewrite setDE indicI indicC/=. + have [/= xyA|/= xyA] := boolP ((x, y) \in _). + rewrite mulr0 EFinN !indicE xyA/= EFinN. + by move/set_mem : xyA => /AB /mem_set ->; rewrite subee. + by rewrite mulr1 EFinN !indicE (negbTE xyA)/= oppr0 adde0. + by split; [exact: measurableD|rewrite IE; exact: emeasurable_funB]. + have lambda_D : lambda_system setT D. + split => //. + - rewrite /D/= indicT/=; split => //. + rewrite [X in measurable_fun _ X](_ : _ = (fun x => k x setT)); last first. + by rewrite /I; apply/funext => x/=; rewrite integral_cst// mul1e. + exact: measurable_kernel. + - suff: D = [set A | (d1, d2).-prod.-measurable A /\ + measurable_fun [set: T1] ((fun C x => k x (xsection C x)) A)]. + by move=> ->; exact: xsection_ndseq_closed. + apply/seteqP; split=> [/= A [mA /= mIA]|/= A [/= mA mIA]]. + split => //. + rewrite [X in measurable_fun _ X](_ : _ = I (EFin \o \1_A))//. + apply/funext => x. + rewrite /I/= integral_indic// ?setIT//. + by rewrite -[X in _ = k x X]/(_ @^-1` _) -xsectionE. + rewrite -[X in measurable X]/(_ @^-1` _) -xsectionE. + exact: measurable_xsection. + split => //. + rewrite /I [X in measurable_fun _ X](_ : _ = (fun x => k x (xsection A x)))//. + apply/funext => x. + rewrite integral_indic//; last first. + rewrite -[X in measurable X]/(_ @^-1` _) -xsectionE. + exact: measurable_xsection. + by rewrite setIT xsectionE. + rewrite /= in lambda_D. + have DE : D = @measurable _ (T1 * T2)%type. + apply/seteqP; split => [/= A []//|]. + rewrite measurable_prod_measurableType. + apply: lambda_system_subset => //. + (* NB: lemma? *) + move=> X Y [X1 mX1 [X2 mX2 <-{X}]] [Y1 mY1 [Y2 mY2 <-{Y}]]. + exists (X1 `&` Y1); first exact: measurableI. + by exists (X2 `&` Y2); [exact: measurableI|rewrite setXI]. + move=> /= C [A mA [B mB] <-]. + split; first exact: measurableX. + rewrite [X in measurable_fun _ X](_ : _ = + (fun s => (\1_A s)%:E * k s B)); last first. + by apply/funext => s; rewrite IgE. + apply: emeasurable_funM; first exact/measurable_EFinP. + exact: measurable_kernel. + have mI1 (A : set (T1 * T2)) : measurable A -> + measurable_fun setT (I (EFin \o \1_A)). + by rewrite -DE => -[]. + rewrite [X in measurable_fun _ X](_ : _ = I + (EFin \o (fun x => \sum_(y \in range (f_ m)) y * + \1_(f_ m @^-1` [set y]) x))%R); last first. + apply/funext => x/=. + by apply: eq_integral => y _ /=; rewrite fimfunE. + rewrite /I/= [X in measurable_fun _ X](_ : _ = (fun x => + \sum_(y \in range (f_ m)) + (\int[k x]_w2 (y * \1_(f_ m @^-1` [set y]) (x, w2))%:E))); last first. + apply/funext => x. + under eq_integral. + move=> y _; rewrite -fsumEFin//. + over. + rewrite /= ge0_integral_fsum//=. + move=> r. + under eq_fun do rewrite EFinM. + apply: emeasurable_funM => //. + by apply/measurable_EFinP; exact: measurableT_comp. + by move=> r y _; rewrite EFinM nnfun_muleindic_ge0. + apply: emeasurable_fsum => // r. + rewrite [X in measurable_fun _ X](_ : _ = (fun x => + (r%:E * \int[k x]_y (\1_(f_ m @^-1` [set r]) (x, y))%:E))); last first. + apply/funext => x. + under eq_integral do rewrite EFinM. + rewrite integralZl//. + pose kx : {measure set T2 -> \bar R} := k x. + pose H1 := isFinite.Build _ _ _ _ + (@finite_finite_transition_kernel _ _ _ _ R k x). + pose H2 := isSFinite.Build _ _ _ _ (finite_measure_sfinite + (@finite_finite_transition_kernel _ _ _ _ R k x)). + pose H3 := isSigmaFinite.Build _ _ _ _ (finite_measure_sigma_finite + (@finite_finite_transition_kernel _ _ _ _ R k x)). + pose kx' := HB.pack_for (FiniteMeasure.type T2 R) (Measure.sort kx) H1 H2 H3. + have kxE : kx = kx' by apply: eq_measure. + rewrite -/kx kxE; apply: integrable_indic => //. + rewrite -[X in measurable X]/(_ @^-1` _) -xsectionE. + exact: measurable_xsection. + by apply: emeasurable_funM => //; exact: mI1. +by move=> ? _; exact: If_cvg. +Qed. + +End measurable_kfcomp. + +Section k1comp. +Context d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType}. + +(* intermediate function used in thm 14.22 *) +Definition k1comp (k : (T0 * T1)%type -> {measure set T2 -> \bar R}) A + := fun xy => (\int[k xy]_z (\1_A (xy.2, z))%:E)%E. + +Lemma measurable_k1comp (k : R.-ftker (T0 * T1) ~> T2) A : measurable A -> + measurable_fun setT (k1comp k A). +Proof. +move=> mA; apply: (@measurable_kfcomp _ _ (T0 * T1)%type T2 R k + (fun abc => (\1_A (abc.1.2, abc.2))%:E)) => //. + apply/measurable_EFinP => //=; apply: measurableT_comp => //=. + apply/prod_measurable_funP; split => /=. + rewrite [X in measurable_fun _ X](_ : _ = snd \o fst)//. + exact: measurableT_comp. + by rewrite [X in measurable_fun _ X](_ : _ = snd). +by move=> t; rewrite lee_fin. +Qed. + +End k1comp. + +(* parameterized composition of two finite transition kernels in thm 14.22 *) +(* NB: not exactly kcomp k1 k2 because of the return type *) +Definition kkcomp_dep d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : T0 -> {measure set T1 -> \bar R}) + (k2 : T0 * T1 -> {measure set T2 -> \bar R}) : T0 -> set (T1 * T2) -> \bar R + := fun (x : T0) (A : set (T1 * T2)) => + (\int[k1 x]_w1 (k1comp k2 A (x, w1)))%E. + +(* (unparameterized) composition of two finite transition kernels *) +Definition kkcomp_undep d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : T0 -> {measure set T1 -> \bar R}) + (k2 : T1 -> {measure set T2 -> \bar R}) : T0 -> set (T1 * T2) -> \bar R + := kkcomp_dep k1 (fun x01 => k2 x01.2). + +Lemma kkcomp_depE d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) A : + (kkcomp_dep k1 k2) ^~ A = + fun x => \int[k1 x]_y (k1comp k2 A) (x, y). +Proof. exact/funext. Qed. + +(* thm 14.22 b *) +Theorem semi_sigma_additive_kkcomp_dep d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) x : + semi_sigma_additive (kkcomp_dep k1 k2 x). +Proof. +move=> /= A mA tA mbigcup. +have /(congr1 (fun f => f x)) -> := kkcomp_depE k1 k2 (\bigcup_n A n). +rewrite (_ : (fun n => _) = (fun n => + \int[k1 x]_w1 (\sum_(0 <= i < n) k1comp k2 (A i) (x, w1)))); last first. + apply/funext => n; rewrite ge0_integral_sum//. + by move=> m; exact: measurable_prod2 (measurable_k1comp k2 (mA m)). + by move=> m y _; apply: integral_ge0 => z _; rewrite lee_fin. +pose g' : nat -> T1 -> \bar R := + fun n y => \sum_(0 <= i < n) (k1comp k2 (A i) (x, y)). +rewrite [X in _ --> X](_ : _ = (\int[k1 x]_y limn (g'^~ y))%E); last first. + apply: eq_integral => y _. + rewrite /g' /k1comp -(@integral_nneseries _ _ R (k2 (x, y)) _ measurableT + (fun i z => (\1_(A i) (y, z))%:E))//. + - by apply: eq_integral => z _ /=; rewrite indic_bigcup. + - move=> n; apply/measurable_EFinP => //; apply: measurable_indic. + rewrite -[X in measurable X]/(_ @^-1` _) -xsectionE. + exact: measurable_xsection. + - by move=> n z _; rewrite lee_fin. +apply: (@cvg_monotone_convergence _ _ _ (k1 x) _ measurableT g') => //. +- move=> n. + apply: (@emeasurable_sum _ _ R setT _ _ + (fun i t => k1comp k2 (A i) (x, t))) => m. + exact: measurable_prod2 (measurable_k1comp k2 (mA m)). +- move=> n y _; apply: sume_ge0 => m _. + by apply: integral_ge0 => z _; rewrite lee_fin. +- move=> y _ a b ab; apply: lee_sum_nneg_natr => // m _ _. + by apply: integral_ge0 => z _; rewrite lee_fin. +Qed. + +Section kkcomp_dep_measure. +Context d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2). + +Let kkcomp_dep0 x : kkcomp_dep k1 k2 x set0 = 0. +Proof. +by apply: integral0_eq => y _; apply: integral0_eq => z _; rewrite indic0. +Qed. + +Let kkcomp_dep_ge0 x A : (0 <= kkcomp_dep k1 k2 x A)%E. +Proof. +by apply: integral_ge0 => y _; apply: integral_ge0 => z _; rewrite lee_fin. +Qed. + +Let kkcomp_dep_additive x : semi_sigma_additive (kkcomp_dep k1 k2 x). +Proof. exact: semi_sigma_additive_kkcomp_dep. Qed. + +HB.instance Definition _ (x : T0) := isMeasure.Build + (measure_prod_display (d1, d2)) (T1 * T2)%type R (kkcomp_dep k1 k2 x) + (kkcomp_dep0 x) (kkcomp_dep_ge0 x) (@kkcomp_dep_additive x). + +Definition mkkcomp_dep := + (kkcomp_dep k1 k2 : T0 -> {measure set (T1 * T2) -> \bar R}). + +End kkcomp_dep_measure. + +(* thm 14.22 a *) +Theorem measurable_kkcomp_dep d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) + A : measurable A -> + measurable_fun [set: T0] ((kkcomp_dep k1 k2) ^~ A). +Proof. +move=> mA; rewrite kkcomp_depE; apply: measurable_kfcomp => // t. + exact: measurable_k1comp. +by apply: integral_ge0 => y _; rewrite lee_fin. +Qed. + +HB.instance Definition _ d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) := + @isKernel.Build _ _ T0 (T1 * T2)%type R + (mkkcomp_dep k1 k2) (measurable_kkcomp_dep k1 k2). + +Section sigma_finite_kkcomp_dep. +Context d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2). + +(* thm 14.22 c: the composition of finite transition kernels is + at least sigma-finite *) +Theorem sigma_finite_kkcomp_dep x : sigma_finite setT (kkcomp_dep k1 k2 x). +Proof. +pose A n := [set w1 | k2 (x, w1) setT < n%:R%:E]. +have mA n : measurable (A n). + rewrite /A -[X in measurable X]setTI. + apply: emeasurable_fun_infty_o => //. + have := @measurable_kernel _ _ _ _ R k2 _ measurableT. + exact: measurable_prod2. +have bigcupA : \bigcup_n A n = setT. + have fink2 xy : fin_num_fun (k2 xy) by exact: finite_finite_transition_kernel. + apply/seteqP; split => // y _. + have {}fink2 := fink2 (x, y) _ measurableT. + exists (Num.trunc (fine (k2 (x, y) setT))).+1 => //=. + have : (0 <= fine (k2 (x, y) [set: T2]))%R by rewrite fine_ge0. + move=> /Num.Theory.trunc_itv/andP[_]. + by rewrite -lte_fin fineK. +have lty n : kkcomp_dep k1 k2 x (A n `*` setT) < +oo. + have fink1 : fin_num_fun (k1 x) by apply: finite_finite_transition_kernel. + apply: (@le_lt_trans _ _ (n%:R%:E * k1 x (A n))) => /=. + rewrite /kkcomp_dep. + rewrite [leLHS](_ : _ = \int[k1 x]_(y in A n) k2 (x, y) setT); last first. + under eq_integral. + move=> y _. + rewrite /k1comp(*NB:lemma?*) integral_indic//; last first. + rewrite -[X in measurable X]/(_ @^-1` _) -xsectionE. + by apply: measurable_xsection; exact: measurableX. + rewrite setIT. + over. + rewrite [RHS]integral_mkcond; apply: eq_integral => y _. + rewrite patchE; case: ifPn => Anx. + congr (k2 _ _); apply/funext => z/=. + by apply/propext; split => // _; split => //; exact/set_mem. + rewrite [X in k2 _ X = _](_ : _ = set0) ?measure0//. + rewrite -subset0// => z/= []. + by move: Anx; rewrite notin_setE. + apply: (@le_trans _ _ (\int[k1 x]_(x in A n) n%:R%:E)). + apply: ge0_le_integral => //. + - apply: measurable_funTS. + have := @measurable_kernel _ _ _ _ _ k2 _ measurableT. + exact: measurable_prod2. + - by move=> y; rewrite /A/= => /ltW. + - by rewrite integral_cst. + by rewrite lte_mul_pinfty// ltey_eq fink1. +exists (fun n => A n `*` setT) => /=. + by rewrite -setX_bigcupl bigcupA setXTT. +by move=> n; split => //; exact: measurableX. +Qed. + +HB.instance Definition _ x := @isSigmaFiniteTransitionKernel.Build d0 + (measure_prod_display (d1, d2)) T0 (T1 * T2)%type R + (mkkcomp_dep k1 k2) sigma_finite_kkcomp_dep. + +End sigma_finite_kkcomp_dep. + +Definition kkcomp d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : (*T0 -> {measure set T1 -> \bar R}*)R.-spker T0 ~> T1) + (k2 : (*T1 -> {measure set T2 -> \bar R}*)R.-spker T1 ~> T2) + : T0 -> set T2 -> \bar R := + fun x A => \int[k1 x]_w1 (k2 w1 A). + +Section spker_snd. +Context d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-spker T1 ~> T2). + +(* ignore the first argument of a parameterized kernel *) +Definition kernel_snd : (T0 * T1)%type -> set T2 -> \bar R := + fun x A => k1 x.2 A. + +Let kernel_snd0 x : kernel_snd x set0 = 0. +Proof. by rewrite /kernel_snd measure0. Qed. + +Let kernel_snd_ge0 x A : (0 <= kernel_snd x A)%E. +Proof. by rewrite /kernel_snd measure_ge0. Qed. + +Let kernel_snd_additive x : semi_sigma_additive (kernel_snd x). +Proof. by move=> F mF tF mUF; exact: measure_semi_sigma_additive. Qed. + +HB.instance Definition _ x := isMeasure.Build + d2 T2 R (kernel_snd x) + (kernel_snd0 x) (kernel_snd_ge0 x) (@kernel_snd_additive x). + +Definition mkernel_snd : (T0 * T1)%type -> {measure set T2 -> \bar R} := + fun x => k1 x.2. + +Let measurable_kernel U : measurable U -> + measurable_fun [set: _] (mkernel_snd ^~ U). +Proof. +move=> mU. +have /= mk1 := measurable_kernel k1 _ mU. +move=> _ /= Y mY. +have {}mk1 := mk1 measurableT _ mY. +have -> : ([set: T0 * T1] `&` (fun x => mkernel_snd x U) @^-1` Y) = + (setT `*` ([set: T1] `&` (k1 ^~ U) @^-1` Y)). + by rewrite !setTI setTX. +exact: measurableX. +Qed. + +HB.instance Definition _ := + @isKernel.Build _ _ _ _ _ mkernel_snd measurable_kernel. + +Let measure_uub : measure_fam_uub mkernel_snd. +Proof. +exists 2%E => /= -[x y]. +rewrite /mkernel_snd/= (@le_lt_trans _ _ 1%:E) ?lte1n//. +exact: sprob_kernel_le1. +Qed. + +HB.instance Definition _ := + @Kernel_isFinite.Build _ _ _ _ _ mkernel_snd measure_uub. + +Let sprob_kernel : + (ereal_sup [set (mkernel_snd) z [set: _] | z in [set: _]] <= 1)%E. +Proof. +by apply: (sprob_kernelP mkernel_snd).2 => -[x y]; exact: sprob_kernel_le1. +Qed. + +HB.instance Definition _ := FiniteKernel_isSubProbability.Build _ _ _ _ _ + mkernel_snd sprob_kernel. + +End spker_snd. + +(* thm 14.26 a *) +Lemma kkcompE d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-spker T0 ~> T1) (k2 : R.-spker T1 ~> T2) x A : + measurable A -> + kkcomp k1 k2 x A = kkcomp_undep k1 k2 x (snd @^-1` A). +Proof. +move=> mA; rewrite /kkcomp /kkcomp_undep /kkcomp_dep. +apply: eq_integral => y _ /=. +by rewrite /k1comp/=(*NB:lemma?*) integral_indic// setIT. +Qed. + +Section kkcomp_measure. +Context d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-spker T0 ~> T1) (k2 : R.-spker T1 ~> T2). + +Let kkcomp0 x : kkcomp k1 k2 x set0 = 0. +Proof. by apply: integral0_eq => y _; rewrite measure0. Qed. + +Let kkcomp_ge0 x A : 0 <= kkcomp k1 k2 x A. +Proof. by apply: integral_ge0 => y _; exact: measure_ge0. Qed. + +Let kkcomp_additive x : semi_sigma_additive (kkcomp k1 k2 x). +Proof. +move=> F mF tF mUF. +rewrite kkcompE// (_ : (fun _ => _) = (fun n => + \sum_(0 <= i < n) kkcomp_undep k1 k2 x (snd @^-1` F i))); last first. + by apply/funext => n; apply: eq_bigr => k _; rewrite kkcompE. +pose F' n := [set: T1] `*` F n. +have kkcomp_bigcup : kkcomp k1 k2 x (\bigcup_n F n) = + kkcomp_dep k1 (mkernel_snd k2) x (\bigcup_n F' n). + apply: eq_integral => y _. + rewrite /k1comp /= integral_indic//; last first. + apply: bigcup_measurable => // k _. + rewrite /F' -[X in measurable X]/(_ @^-1` _) -xsectionE. + by apply: measurable_xsection; exact: measurableX. + congr (k2 y _). + apply/seteqP; split => [z [i _ Fiz] /=|z/= [[i _ Fiz]] _]. + by split => //; exists i. + by exists i => //; case: Fiz. +rewrite -kkcompE// kkcomp_bigcup. +rewrite (_ : (fun n => _) = (fun n => + \sum_(0 <= i < n) kkcomp_dep k1 (mkernel_snd k2) x (F' i))); last first. + apply/funext => n; apply: eq_bigr => i _. + apply: eq_integral => y _; apply: eq_integral => z _. + by rewrite /F' setTX. +apply: semi_sigma_additive_kkcomp_dep => //. +- by move=> i; exact: measurableX. +- apply/trivIsetP => i j _ _ ij. + rewrite /F' -setXI setTI. + move/trivIsetP : tF => /(_ i j Logic.I Logic.I ij) ->. + by rewrite setX0. +- by apply: bigcup_measurable => k _; exact: measurableX. +Qed. + +HB.instance Definition _ x := isMeasure.Build + d2 T2 R (kkcomp k1 k2 x) (kkcomp0 x) (kkcomp_ge0 x) (@kkcomp_additive x). + +Definition mkkcomp := (kkcomp k1 k2 : T0 -> {measure set T2 -> \bar R}). + +Let measurable_kernel U : + measurable U -> measurable_fun [set: _] (mkkcomp ^~ U). +Proof. +move=> mU. +rewrite [X in measurable_fun _ X](_ : _ = + (kkcomp_dep k1 (mkernel_snd k2))^~ ([set: T1] `*` U)); last first. + apply/funext => x. + have := @kkcomp_depE _ _ _ T0 T1 T2 R k1 (mkernel_snd k2) (setT `*` U). + move=> /(congr1 (fun f => f x)) ->. + apply: eq_integral => y _. + rewrite /k1comp/= integral_indic//; last first. + rewrite -[X in measurable X]/(_ @^-1` _) -xsectionE. + by apply: measurable_xsection; exact: measurableX. + rewrite /mkernel_snd/= setIT; congr (k2 y _). + by apply/seteqP; split => // ? []. +by apply: measurable_kkcomp_dep; exact: measurableX. +Qed. + +HB.instance Definition _ := @isKernel.Build _ _ _ _ R mkkcomp measurable_kernel. + +End kkcomp_measure. + +(* the composition of subprobability kernels is a subprobability kernel *) +Section subprob_kkcomp. +Context d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-spker T0 ~> T1) (k2 : R.-spker T1 ~> T2). + +(* thm 14.26 b *) +Lemma subprob_kkcomp x : (mkkcomp k1 k2 x setT <= 1)%E. +Proof. +rewrite /mkkcomp [leLHS]kkcompE// preimage_setT. +rewrite /kkcomp_undep /kkcomp_dep /k1comp/=. +rewrite [leLHS](_ : _ = \int[k1 x]_w1 (k2 w1 setT)); last first. + apply: eq_integral => y _. + rewrite integral_indic//; last first. + rewrite [X in measurable X](_ : _ = ysection setT y). + exact: measurable_ysection. + by rewrite ysectionE. + by rewrite setIT. +apply: (@le_trans _ _ (\int[k1 x]__ 1)); last first. + by rewrite integral_cst// mul1e; exact: sprob_kernel_le1. +apply: ge0_le_integral => //; first exact: measurable_kernel. +by move=> y _; exact: sprob_kernel_le1. +Qed. + +Let sprob_kernel : + ereal_sup [set (mkkcomp k1 k2) x setT | x in setT] <= 1. +Proof. by apply/sprob_kernelP => x; exact: subprob_kkcomp. Qed. + +HB.instance Definition _ := Kernel_isSubProbability.Build + _ _ _ _ R (mkkcomp k1 k2) sprob_kernel. + +End subprob_kkcomp. diff --git a/theories/measure.v b/theories/measure.v index cc1637134..6f688297a 100644 --- a/theories/measure.v +++ b/theories/measure.v @@ -4206,7 +4206,7 @@ Lemma le_outer_measure : {homo mu : A B / A `<=` B >-> A <= B}. Proof. move=> A B AB; pose B_ k := if k is 0%N then B else set0. have -> : mu B = \sum_(n ?; rewrite outer_measure_ge0. + rewrite nneseries_recl//; last by move=> ? ?; rewrite outer_measure_ge0. rewrite eseries_cond/= eseries0 ?adde0// => -[|]//= k _ _. by rewrite outer_measure0. apply: subset_outer_measure_sigma_subadditive => //. diff --git a/theories/numfun.v b/theories/numfun.v index acb61398b..353cab228 100644 --- a/theories/numfun.v +++ b/theories/numfun.v @@ -311,6 +311,9 @@ Proof. by apply/funext=> x; rewrite indicE in_set0. Qed. Lemma indicI A B : \1_(A `&` B) = \1_A \* \1_B :> (_ -> R). Proof. by apply/funext=> u/=; rewrite !indicE in_setI -natrM mulnb. Qed. +Lemma indicC A : \1_(~` A) = (fun x => (~~ (x \in A))%:R) :> (_ -> R). +Proof. by apply/funext=> u/=; rewrite indicE in_setC. Qed. + Lemma image_indic D A : \1_D @` A = (if A `\` D != set0 then [set 0] else set0) `|` (if A `&` D != set0 then [set 1 : R] else set0). @@ -363,6 +366,25 @@ Qed. End indic_lemmas. +Lemma indic_bigcup T {R : realType} (A : (set T)^nat) (t : T) : + trivIset [set: nat] A -> + (\1_(\bigcup_n A n) t)%:E = (\sum_(0 <= n \bar R. +Proof. +move=> tA. +have [At|At] := eqVneq (\1_(\bigcup_n A n) t) (1%R : R). +- move: (At) => /eqP; rewrite pnatr_eq1 eqb1 => /asboolP[i _] Ait. + rewrite At (@nneseriesD1 _ _ i)//; last by move=> j; rewrite lee_fin. + rewrite indicE mem_set// eseries0 ?adde0// => j _/= ji. + rewrite indicE memNset// => Ajt. + move/trivIsetP : tA => /(_ j i Logic.I Logic.I ji). + by apply/eqP/set0P; exists t. +- have {}At : \1_(\bigcup_n A n) t = 0%R :> R. + by apply/eqP; move: At; rewrite pnatr_eq1 eqb1 pnatr_eq0 eqb0. + move: (At) => /eqP; rewrite pnatr_eq0 eqb0 notin_setE => AtC. + rewrite At eseries0// => j _ _. + by rewrite indicE memNset// => Ajt; apply: AtC; exists j. +Qed. + Lemma patch_indic T {R : numFieldType} (f : T -> R) (D : set T) : f \_ D = (f \* \1_D)%R. Proof. diff --git a/theories/sequences.v b/theories/sequences.v index 285fb6255..220622036 100644 --- a/theories/sequences.v +++ b/theories/sequences.v @@ -1921,7 +1921,7 @@ rewrite -lim_shift_cst; last by rewrite (@lt_le_trans _ _ 0)// f0// leq_addr. Unshelve. all: by end_near. Qed. Lemma nneseries_split_cond (R : realType) (f : nat -> \bar R) N n (P : pred nat) : - (forall k, P k -> 0 <= f k)%E -> + (forall k, P k -> 0 <= f k) -> \sum_(N <= k k Nk. by case: ifPn => //; exact: NPf. Qed. +Lemma nneseriesD1 {R : realType} (f : nat -> \bar R) n (P : pred nat) : + (forall k, P k -> 0 <= f k) -> P n -> + \sum_(0 <= k f0 Pn. +rewrite (@nneseries_split_cond _ f 0%N n.+1 P)// add0n big_mkcond/=. +rewrite big_nat_recr//= Pn -big_mkcond/= -addrA addrCA; congr +%E. +rewrite [RHS]eseries_mkcondr. +rewrite [in RHS](@nneseries_split_cond _ _ _ n.+1 P)//; last first. + by move=> k Pk; case: ifPn => // _; exact: f0. +rewrite add0n [X in _ = X + _]big_mkcond/= big_nat_recr//= Pn eqxx/= adde0. +rewrite -big_mkcond//=; congr +%E. + rewrite big_seq_cond [RHS]big_seq_cond; apply: eq_bigr => /= i. + by rewrite mem_index_iota leq0n/= => /andP[ij Pi]; rewrite lt_eqF. +rewrite eseries_cond [RHS]eseries_cond; apply: eq_eseriesr => i /andP[Pi ji]. +by rewrite gt_eqF. +Qed. + End nneseries_split. Arguments nneseries_split {R f} _ _. Arguments nneseries_split_cond {R f} _ _ _. +Arguments nneseriesD1 {R f} n {P}. -Lemma nneseries_recl (R : realType) (f : nat -> \bar R) : - (forall k, 0 <= f k) -> \sum_(k \bar R) : + (forall k, P k -> 0 <= f k) -> P 0%N -> + \sum_(0 <= k f0; rewrite [LHS](nneseries_split _ 1)// add0n. -by rewrite /index_iota subn0/= big_cons big_nil addr0. +move=> F0 P0; rewrite (nneseriesD1 0%N)//; congr +%E. +by rewrite [RHS]eseries_cond; apply: eq_eseriesl => n; rewrite lt0n. Qed. Lemma nneseries_tail_cvg (R : realType) (f : (\bar R)^nat) P : From e3aa56ded575eee11aaeaf8026c4c90ae39ff1ff Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Mon, 30 Dec 2024 17:57:14 +0900 Subject: [PATCH 02/36] add sigma finite kernels --- theories/kernel.v | 57 +++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/theories/kernel.v b/theories/kernel.v index 7983a18c5..ed3c697d5 100644 --- a/theories/kernel.v +++ b/theories/kernel.v @@ -190,8 +190,24 @@ have ? : m1 = m2. by subst m1; f_equal; f_equal; f_equal; apply/Prop_irrelevance. Qed. +(* interface for sigma-finite kernel *) +HB.mixin Record SFiniteKernel_isSigmaFinite + d d' (X : measurableType d) (Y : measurableType d') + (R : realType) (k : X -> {measure set Y -> \bar R}) := { + sigma_finite_finite_transition_kernel : forall x, sigma_finite setT (k x) }. + +#[short(type=sigma_finite_kernel)] +HB.structure Definition SigmaFiniteKernel + d d' (X : measurableType d) (Y : measurableType d') (R : realType) := + { k of @SFiniteKernel _ _ _ _ _ k & + SFiniteKernel_isSigmaFinite _ _ X Y R k }. + +Reserved Notation "R .-sigmafinker X ~> Y" + (at level 42, format "R .-sigmafinker X ~> Y"). +Notation "R .-sigmafinker X ~> Y" := (sigma_finite_kernel X%type Y R). + (* interface for finite transition kernel *) -HB.mixin Record SFiniteKernel_isFiniteTransition +HB.mixin Record SigmaFiniteKernel_isFiniteTransition d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : X -> {measure set Y -> \bar R}) := { finite_finite_transition_kernel : forall x, fin_num_fun (k x) }. @@ -199,8 +215,8 @@ HB.mixin Record SFiniteKernel_isFiniteTransition #[short(type=finite_transition_kernel)] HB.structure Definition FiniteTransitionKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) := - { k of @SFiniteKernel _ _ _ _ _ k & - SFiniteKernel_isFiniteTransition _ _ X Y R k }. + { k of @SigmaFiniteKernel _ _ _ _ _ k & + SigmaFiniteKernel_isFiniteTransition _ _ X Y R k }. Notation "R .-ftker X ~> Y" := (finite_transition_kernel X%type Y R). @@ -257,6 +273,18 @@ Qed. HB.instance Definition _ := @Kernel_isSFinite_subdef.Build d d' X Y R k sfinite_finite. +(* TODO: renamed *) +Let sigma_finite_finite_transition_kernel x : sigma_finite setT (k x). +Proof. +apply: fin_num_fun_sigma_finite; first by rewrite measure0. +apply: lty_fin_num_fun. +have [r kr] := measure_uub. +by rewrite (lt_trans (kr x)) ?ltry. +Qed. + +HB.instance Definition _ := + @SFiniteKernel_isSigmaFinite.Build d d' X Y R k sigma_finite_finite_transition_kernel. + Let finite_transition_finite : forall x, fin_num_fun (k x). Proof. move=> x U mU. @@ -267,7 +295,7 @@ by rewrite (lt_trans (kr x)) ?ltry. Qed. HB.instance Definition _ := - @SFiniteKernel_isFiniteTransition.Build d d' X Y R k finite_transition_finite. + @SigmaFiniteKernel_isFiniteTransition.Build d d' X Y R k finite_transition_finite. HB.instance Definition _ := @FiniteTransitionKernel_isFinite.Build d d' X Y R k measure_uub. @@ -1154,20 +1182,6 @@ Proof. by move=> mf; exact: measurableT_comp. Qed. End measurable_fun_prod. -HB.mixin Record isSigmaFiniteTransitionKernel - d d' (X : measurableType d) (Y : measurableType d') - (R : realType) (k : X -> {measure set Y -> \bar R}) := { - sigma_finite_finite_transition_kernel : forall x, sigma_finite setT (k x) }. - -#[short(type=sigma_finite_transition_kernel)] -HB.structure Definition SigmaFiniteTransitionKernel - d d' (X : measurableType d) (Y : measurableType d') (R : realType) := - { k of @Kernel _ _ _ _ _ k & isSigmaFiniteTransitionKernel _ _ X Y R k }. - -Reserved Notation "R .-sftker X ~> Y" - (at level 42, format "R .-sftker X ~> Y"). -Notation "R .-sftker X ~> Y" := (sigma_finite_transition_kernel X%type Y R). - Lemma sprob_kernel_le1 d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : R.-spker X ~> Y) x : k x [set: Y] <= 1. @@ -1384,8 +1398,7 @@ Definition kkcomp_dep d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} (k1 : T0 -> {measure set T1 -> \bar R}) (k2 : T0 * T1 -> {measure set T2 -> \bar R}) : T0 -> set (T1 * T2) -> \bar R - := fun (x : T0) (A : set (T1 * T2)) => - (\int[k1 x]_w1 (k1comp k2 A (x, w1)))%E. + := fun x (A : set (T1 * T2)) => \int[k1 x]_y (k1comp k2 A (x, y)). (* (unparameterized) composition of two finite transition kernels *) Definition kkcomp_undep d0 d1 d2 (T0 : measurableType d0) @@ -1536,7 +1549,7 @@ exists (fun n => A n `*` setT) => /=. by move=> n; split => //; exact: measurableX. Qed. -HB.instance Definition _ x := @isSigmaFiniteTransitionKernel.Build d0 +HB.instance Definition _ x := @SFiniteKernel_isSigmaFinite.Build d0 (measure_prod_display (d1, d2)) T0 (T1 * T2)%type R (mkkcomp_dep k1 k2) sigma_finite_kkcomp_dep. @@ -1547,7 +1560,7 @@ Definition kkcomp d0 d1 d2 (T0 : measurableType d0) (k1 : (*T0 -> {measure set T1 -> \bar R}*)R.-spker T0 ~> T1) (k2 : (*T1 -> {measure set T2 -> \bar R}*)R.-spker T1 ~> T2) : T0 -> set T2 -> \bar R := - fun x A => \int[k1 x]_w1 (k2 w1 A). + fun x A => \int[k1 x]_y (k2 y A). Section spker_snd. Context d0 d1 d2 (T0 : measurableType d0) From 503a95c16a8a000cba5a55baad37d3db1e719122 Mon Sep 17 00:00:00 2001 From: Cyril Cohen Date: Mon, 30 Dec 2024 10:46:36 +0100 Subject: [PATCH 03/36] rename and deprecate --- theories/kernel.v | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/theories/kernel.v b/theories/kernel.v index ed3c697d5..bfd5de5b5 100644 --- a/theories/kernel.v +++ b/theories/kernel.v @@ -220,16 +220,28 @@ HB.structure Definition FiniteTransitionKernel Notation "R .-ftker X ~> Y" := (finite_transition_kernel X%type Y R). -HB.mixin Record FiniteTransitionKernel_isFinite d d' +HB.mixin Record isMeasureFamUub d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : X -> {measure set Y -> \bar R}) := { measure_uub : measure_fam_uub k }. +#[deprecated(since="mathcomp-analysis 1.9.0", + note="Use isMeasureFamUub instead.")] +Notation SFiniteKernel_isFinite x1 x2 x3 x4 x5 x6 := + (isMeasureFamUub x1 x2 x3 x4 x5 x6). + +Module SFiniteKernel_isFinite. +#[deprecated(since="mathcomp-analysis 1.9.0", + note="Use isMeasureFamUub.Build instead.")] +Notation Build x1 x2 x3 x4 x5 x6 := + (isMeasureFamUub.Build x1 x2 x3 x4 x5 x6) (only parsing). +End SFiniteKernel_isFinite. + #[short(type=finite_kernel)] HB.structure Definition FiniteKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) := { k of @FiniteTransitionKernel _ _ _ _ _ k & - FiniteTransitionKernel_isFinite _ _ X Y R k }. + isMeasureFamUub _ _ X Y R k }. Notation "R .-fker X ~> Y" := (finite_kernel X%type Y R). Arguments measure_uub {_ _ _ _ _} _. @@ -298,7 +310,7 @@ HB.instance Definition _ := @SigmaFiniteKernel_isFiniteTransition.Build d d' X Y R k finite_transition_finite. HB.instance Definition _ := - @FiniteTransitionKernel_isFinite.Build d d' X Y R k measure_uub. + @isMeasureFamUub.Build d d' X Y R k measure_uub. HB.end. @@ -1590,10 +1602,8 @@ Definition mkernel_snd : (T0 * T1)%type -> {measure set T2 -> \bar R} := Let measurable_kernel U : measurable U -> measurable_fun [set: _] (mkernel_snd ^~ U). Proof. -move=> mU. -have /= mk1 := measurable_kernel k1 _ mU. -move=> _ /= Y mY. -have {}mk1 := mk1 measurableT _ mY. +move=> mU; have /= mk1 := measurable_kernel k1 _ mU. +move=> _ /= Y mY; have {}mk1 := mk1 measurableT _ mY. have -> : ([set: T0 * T1] `&` (fun x => mkernel_snd x U) @^-1` Y) = (setT `*` ([set: T1] `&` (k1 ^~ U) @^-1` Y)). by rewrite !setTI setTX. From 320a97684d33a191486c0ab76fda579928ae5aa2 Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Tue, 31 Dec 2024 02:04:11 +0900 Subject: [PATCH 04/36] renaming --- theories/kernel.v | 505 +++++++++++++++++++++++++--------------------- 1 file changed, 271 insertions(+), 234 deletions(-) diff --git a/theories/kernel.v b/theories/kernel.v index bfd5de5b5..0f732fc02 100644 --- a/theories/kernel.v +++ b/theories/kernel.v @@ -25,6 +25,10 @@ From mathcomp Require Import numfun lebesgue_measure lebesgue_integral. (* measure_fam_uub k == the kernel k is uniformly upper-bounded *) (* R.-sfker X ~> Y == s-finite kernel *) (* The HB class is SFiniteKernel. *) +(* R.-sigmafker X ~> Y == sigma-finite kernel *) +(* The HB class is SigmaFiniteKernel. *) +(* R.-ftker X ~> Y == finite transition kernel *) +(* The HB class is FiniteTransitionKernel. *) (* R.-fker X ~> Y == finite kernel *) (* The HB class is FiniteKernel. *) (* R.-spker X ~> Y == subprobability kernel *) @@ -58,10 +62,12 @@ Reserved Notation "R .-ker X ~> Y" (at level 42, format "R .-ker X ~> Y"). Reserved Notation "R .-sfker X ~> Y" (at level 42, format "R .-sfker X ~> Y"). -Reserved Notation "R .-fker X ~> Y" - (at level 42, format "R .-fker X ~> Y"). +Reserved Notation "R .-sigmafker X ~> Y" + (at level 42, format "R .-sigmafker X ~> Y"). Reserved Notation "R .-ftker X ~> Y" (at level 42, format "R .-ftker X ~> Y"). +Reserved Notation "R .-fker X ~> Y" + (at level 42, format "R .-fker X ~> Y"). Reserved Notation "R .-spker X ~> Y" (at level 42, format "R .-spker X ~> Y"). Reserved Notation "R .-pker X ~> Y" @@ -166,17 +172,29 @@ Qed. End measure_fam_uub. -HB.mixin Record Kernel_isSFinite_subdef d d' +HB.mixin Record isSFiniteKernel_subdef d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : X -> {measure set Y -> \bar R}) := { sfinite_kernel_subdef : exists2 s : (R.-ker X ~> Y)^nat, forall n, measure_fam_uub (s n) & forall x U, measurable U -> k x U = kseries s x U }. +#[deprecated(since="mathcomp-analysis 1.9.0", + note="Use isSFiniteKernel_subdef instead.")] +Notation Kernel_isSFinite_subdef x1 x2 x3 x4 x5 x6 := + (isSFiniteKernel_subdef x1 x2 x3 x4 x5 x6). + +Module Kernel_isSFinite_subdef. +#[deprecated(since="mathcomp-analysis 1.9.0", + note="Use isSFiniteKernel_subdef.Build instead.")] +Notation Build x1 x2 x3 x4 x5 x6 := + (isSFiniteKernel_subdef.Build x1 x2 x3 x4 x5 x6) (only parsing). +End Kernel_isSFinite_subdef. + HB.structure Definition SFiniteKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) := { k of @Kernel _ _ _ _ R k & - Kernel_isSFinite_subdef _ _ X Y R k }. + isSFiniteKernel_subdef _ _ X Y R k }. Notation "R .-sfker X ~> Y" := (SFiniteKernel.type X%type Y R). Arguments sfinite_kernel_subdef {_ _ _ _ _} _. @@ -190,33 +208,48 @@ have ? : m1 = m2. by subst m1; f_equal; f_equal; f_equal; apply/Prop_irrelevance. Qed. +Section kzero. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). + +Definition kzero (_ : X) : {measure set Y -> \bar R} := mzero. + +Let measurable_fun_kzero U : measurable U -> + measurable_fun [set: X] (kzero ^~ U). +Proof. by move=> ?/=; exact: measurable_cst. Qed. + +HB.instance Definition _ := + @isKernel.Build _ _ X Y R kzero measurable_fun_kzero. + +Lemma kzero_uub : measure_fam_uub kzero. +Proof. by exists 1%R => /= t; rewrite /mzero/=. Qed. + +End kzero. + (* interface for sigma-finite kernel *) -HB.mixin Record SFiniteKernel_isSigmaFinite +HB.mixin Record isSigmaFiniteKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : X -> {measure set Y -> \bar R}) := { - sigma_finite_finite_transition_kernel : forall x, sigma_finite setT (k x) }. + kernel_sigma_finite : forall x, sigma_finite [set: Y] (k x) }. #[short(type=sigma_finite_kernel)] HB.structure Definition SigmaFiniteKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) := - { k of @SFiniteKernel _ _ _ _ _ k & - SFiniteKernel_isSigmaFinite _ _ X Y R k }. + { k of @Kernel _ _ _ _ _ k & + isSigmaFiniteKernel _ _ X Y R k }. -Reserved Notation "R .-sigmafinker X ~> Y" - (at level 42, format "R .-sigmafinker X ~> Y"). -Notation "R .-sigmafinker X ~> Y" := (sigma_finite_kernel X%type Y R). +Notation "R .-sigmafker X ~> Y" := (sigma_finite_kernel X%type Y R). (* interface for finite transition kernel *) -HB.mixin Record SigmaFiniteKernel_isFiniteTransition +HB.mixin Record isFiniteTransitionKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : X -> {measure set Y -> \bar R}) := { - finite_finite_transition_kernel : forall x, fin_num_fun (k x) }. + kernel_finite_transition : forall x, fin_num_fun (k x) }. #[short(type=finite_transition_kernel)] HB.structure Definition FiniteTransitionKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) := - { k of @SigmaFiniteKernel _ _ _ _ _ k & - SigmaFiniteKernel_isFiniteTransition _ _ X Y R k }. + { k of @SFiniteKernel _ _ _ _ _ k & + isFiniteTransitionKernel _ _ X Y R k }. Notation "R .-ftker X ~> Y" := (finite_transition_kernel X%type Y R). @@ -250,23 +283,6 @@ HB.factory Record Kernel_isFinite d d' (k : X -> {measure set Y -> \bar R}) of isKernel _ _ _ _ _ k := { measure_uub : measure_fam_uub k }. -Section kzero. -Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). - -Definition kzero (_ : X) : {measure set Y -> \bar R} := mzero. - -Let measurable_fun_kzero U : measurable U -> - measurable_fun [set: X] (kzero ^~ U). -Proof. by move=> ?/=; exact: measurable_cst. Qed. - -HB.instance Definition _ := - @isKernel.Build _ _ X Y R kzero measurable_fun_kzero. - -Lemma kzero_uub : measure_fam_uub kzero. -Proof. by exists 1%R => /= t; rewrite /mzero/=. Qed. - -End kzero. - HB.builders Context d d' (X : measurableType d) (Y : measurableType d') (R : realType) k of Kernel_isFinite d d' X Y R k. @@ -283,10 +299,9 @@ by rewrite eseries0// adde0. Qed. HB.instance Definition _ := - @Kernel_isSFinite_subdef.Build d d' X Y R k sfinite_finite. + @isSFiniteKernel_subdef.Build d d' X Y R k sfinite_finite. -(* TODO: renamed *) -Let sigma_finite_finite_transition_kernel x : sigma_finite setT (k x). +(*Let kernel_sigma_finite x : sigma_finite setT (k x). Proof. apply: fin_num_fun_sigma_finite; first by rewrite measure0. apply: lty_fin_num_fun. @@ -295,7 +310,7 @@ by rewrite (lt_trans (kr x)) ?ltry. Qed. HB.instance Definition _ := - @SFiniteKernel_isSigmaFinite.Build d d' X Y R k sigma_finite_finite_transition_kernel. + @isSigmaFiniteKernel.Build d d' X Y R k kernel_sigma_finite.*) Let finite_transition_finite : forall x, fin_num_fun (k x). Proof. @@ -307,7 +322,7 @@ by rewrite (lt_trans (kr x)) ?ltry. Qed. HB.instance Definition _ := - @SigmaFiniteKernel_isFiniteTransition.Build d d' X Y R k finite_transition_finite. + @isFiniteTransitionKernel.Build d d' X Y R k finite_transition_finite. HB.instance Definition _ := @isMeasureFamUub.Build d d' X Y R k measure_uub. @@ -365,13 +380,13 @@ HB.factory Record Kernel_isSFinite d d' HB.builders Context d d' (X : measurableType d) (Y : measurableType d') (R : realType) k of Kernel_isSFinite d d' X Y R k. -Lemma sfinite_subdef : Kernel_isSFinite_subdef d d' X Y R k. +Lemma sfinite_subdef : isSFiniteKernel_subdef d d' X Y R k. Proof. split; have [s sE] := sfinite; exists s => //. by move=> n; exact: measure_uub. Qed. -HB.instance Definition _ := (*@isSFinite0.Build d d' X Y R k*) sfinite_subdef. +HB.instance Definition _ := sfinite_subdef. HB.end. @@ -404,19 +419,31 @@ by rewrite nneseries_esum// fun_true; exact: eq_esum. Qed. HB.instance Definition _ := - Kernel_isSFinite_subdef.Build _ _ _ _ R (kseries k) sfinite_kseries. + isSFiniteKernel_subdef.Build _ _ _ _ R (kseries k) sfinite_kseries. End sfkseries. -HB.mixin Record FiniteKernel_isSubProbability d d' +HB.mixin Record isSubProbabilityKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : X -> {measure set Y -> \bar R}) := { sprob_kernel : ereal_sup [set k x [set: Y] | x in [set: X]] <= 1 }. +#[deprecated(since="mathcomp-analysis 1.9.0", + note="Use isSubProbabilityKernel instead.")] +Notation FiniteKernel_isSubProbability x1 x2 x3 x4 x5 x6 := + (isSubProbabilityKernel x1 x2 x3 x4 x5 x6). + +Module FiniteKernel_isSubProbability. +#[deprecated(since="mathcomp-analysis 1.9.0", + note="Use isSubProbabilityKernel.Build instead.")] +Notation Build x1 x2 x3 x4 x5 x6 := + (isSubProbabilityKernel.Build x1 x2 x3 x4 x5 x6) (only parsing). +End FiniteKernel_isSubProbability. + #[short(type=sprobability_kernel)] HB.structure Definition SubProbabilityKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) := { k of @FiniteKernel _ _ _ _ _ k & - FiniteKernel_isSubProbability _ _ X Y R k }. + isSubProbabilityKernel _ _ X Y R k }. Notation "R .-spker X ~> Y" := (sprobability_kernel X%type Y R). Lemma sprob_kernelP d d' (X : measurableType d) (Y : measurableType d') @@ -445,20 +472,32 @@ Qed. HB.instance Definition _ := finite. HB.instance Definition _ := - @FiniteKernel_isSubProbability.Build _ _ _ _ _ k sprob_kernel. + @isSubProbabilityKernel.Build _ _ _ _ _ k sprob_kernel. HB.end. -HB.mixin Record SubProbability_isProbability d d' +HB.mixin Record isProbabilityKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : X -> {measure set Y -> \bar R}) := { prob_kernel : forall x, k x [set: Y] = 1 }. +#[deprecated(since="mathcomp-analysis 1.9.0", + note="Use isProbabilityKernel instead.")] +Notation SubProbability_isProbability x1 x2 x3 x4 x5 x6 := + (isProbabilityKernel x1 x2 x3 x4 x5 x6). + +Module SubProbability_isProbability. +#[deprecated(since="mathcomp-analysis 1.9.0", + note="Use isProbabilityKernel.Build instead.")] +Notation Build x1 x2 x3 x4 x5 x6 := + (isProbabilityKernel.Build x1 x2 x3 x4 x5 x6) (only parsing). +End SubProbability_isProbability. + #[short(type=probability_kernel)] HB.structure Definition ProbabilityKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) := { k of @SubProbabilityKernel _ _ _ _ _ k & - SubProbability_isProbability _ _ X Y R k }. + isProbabilityKernel _ _ X Y R k }. Notation "R .-pker X ~> Y" := (probability_kernel X%type Y R). HB.factory Record Kernel_isProbability d d' @@ -477,7 +516,7 @@ Qed. HB.instance Definition _ := sprob_kernel. HB.instance Definition _ := - @SubProbability_isProbability.Build _ _ _ _ _ k prob_kernel. + @isProbabilityKernel.Build _ _ _ _ _ k prob_kernel. HB.end. @@ -775,7 +814,7 @@ by rewrite -/(measure_add (f1 n x) (f2 n x)) measure_addE. Qed. HB.instance Definition _ t := - Kernel_isSFinite_subdef.Build _ _ _ _ R (kadd k1 k2) sfinite_kadd. + isSFiniteKernel_subdef.Build _ _ _ _ R (kadd k1 k2) sfinite_kadd. End sfkadd. Section fkadd. @@ -871,13 +910,16 @@ apply: measurable_fun_if => //. + by apply: measurableT_comp => //; exact/measurable_funS/measurable_kernel. Qed. +(* parameterized composition of kernels + return type: X -> set Z -> \bar R, + as used in staton2017esop *) Section kcomp_def. Context d1 d2 d3 (X : measurableType d1) (Y : measurableType d2) (Z : measurableType d3) (R : realType). Variable l : X -> {measure set Y -> \bar R}. Variable k : X * Y -> {measure set Z -> \bar R}. -Definition kcomp x U := \int[l x]_y k (x, y) U. +Definition kcomp x (U : set Z) := \int[l x]_y k (x, y) U. End kcomp_def. @@ -1275,11 +1317,11 @@ apply: (emeasurable_fun_cvg (fun n => I (EFin \o f_ n))). rewrite /I. pose kx : {measure set T2 -> \bar R} := k x. pose H1 := isFinite.Build _ _ _ _ - (@finite_finite_transition_kernel _ _ _ _ R k x). + (@kernel_finite_transition _ _ _ _ R k x). pose H2 := isSFinite.Build _ _ _ _ (finite_measure_sfinite - (@finite_finite_transition_kernel _ _ _ _ R k x)). + (@kernel_finite_transition _ _ _ _ R k x)). pose H3 := isSigmaFinite.Build _ _ _ _ (finite_measure_sigma_finite - (@finite_finite_transition_kernel _ _ _ _ R k x)). + (@kernel_finite_transition _ _ _ _ R k x)). pose kx' := HB.pack_for (FiniteMeasure.type T2 R) (Measure.sort kx) H1 H2 H3. have kxE : kx = kx' by exact: eq_measure. rewrite (*TODO: use RintegralB when merged*) -integralB_EFin//; last 2 first. @@ -1365,11 +1407,11 @@ apply: (emeasurable_fun_cvg (fun n => I (EFin \o f_ n))). rewrite integralZl//. pose kx : {measure set T2 -> \bar R} := k x. pose H1 := isFinite.Build _ _ _ _ - (@finite_finite_transition_kernel _ _ _ _ R k x). + (@kernel_finite_transition _ _ _ _ R k x). pose H2 := isSFinite.Build _ _ _ _ (finite_measure_sfinite - (@finite_finite_transition_kernel _ _ _ _ R k x)). + (@kernel_finite_transition _ _ _ _ R k x)). pose H3 := isSigmaFinite.Build _ _ _ _ (finite_measure_sigma_finite - (@finite_finite_transition_kernel _ _ _ _ R k x)). + (@kernel_finite_transition _ _ _ _ R k x)). pose kx' := HB.pack_for (FiniteMeasure.type T2 R) (Measure.sort kx) H1 H2 H3. have kxE : kx = kx' by apply: eq_measure. rewrite -/kx kxE; apply: integrable_indic => //. @@ -1381,139 +1423,153 @@ Qed. End measurable_kfcomp. -Section k1comp. +(* product of kernels + as in klenke2014 + return type : T0 -> set (T1 * T2) -> \bar R *) +Section kproduct_def. +Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) + (T2 : measurableType d2) {R : realType}. +Variable k1 : T0 -> {measure set T1 -> \bar R}. +Variable k2 : T0 * T1 -> {measure set T2 -> \bar R}. + +Definition intker_indic (k : T0 * T1 -> {measure set T2 -> \bar R}) A := + fun xy => \int[k xy]_z (\1_A (xy.2, z))%:E. + +Definition kproduct x (A : set (T1 * T2)) := + \int[k1 x]_y intker_indic k2 A (x, y). + +End kproduct_def. + +(* (unparameterized) product *) +Definition kproduct_snd d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : T0 -> {measure set T1 -> \bar R}) + (k2 : T1 -> {measure set T2 -> \bar R}) : T0 -> set (T1 * T2) -> \bar R := + kproduct k1 (k2 \o snd). + +Section intker_indic_lemmas. Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType}. +Variable k : R.-ftker T0 * T1 ~> T2. -(* intermediate function used in thm 14.22 *) -Definition k1comp (k : (T0 * T1)%type -> {measure set T2 -> \bar R}) A - := fun xy => (\int[k xy]_z (\1_A (xy.2, z))%:E)%E. - -Lemma measurable_k1comp (k : R.-ftker (T0 * T1) ~> T2) A : measurable A -> - measurable_fun setT (k1comp k A). +Lemma measurable_intker_indic A : measurable A -> + measurable_fun [set: T0 * T1] (intker_indic k A). Proof. move=> mA; apply: (@measurable_kfcomp _ _ (T0 * T1)%type T2 R k - (fun abc => (\1_A (abc.1.2, abc.2))%:E)) => //. - apply/measurable_EFinP => //=; apply: measurableT_comp => //=. + (fun abc => (\1_A (abc.1.2, abc.2))%:E)). +- apply/measurable_EFinP => //=; apply: measurableT_comp => //=. apply/prod_measurable_funP; split => /=. rewrite [X in measurable_fun _ X](_ : _ = snd \o fst)//. exact: measurableT_comp. by rewrite [X in measurable_fun _ X](_ : _ = snd). -by move=> t; rewrite lee_fin. +- by move=> t; rewrite lee_fin. Qed. -End k1comp. - -(* parameterized composition of two finite transition kernels in thm 14.22 *) -(* NB: not exactly kcomp k1 k2 because of the return type *) -Definition kkcomp_dep d0 d1 d2 (T0 : measurableType d0) - (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} - (k1 : T0 -> {measure set T1 -> \bar R}) - (k2 : T0 * T1 -> {measure set T2 -> \bar R}) : T0 -> set (T1 * T2) -> \bar R - := fun x (A : set (T1 * T2)) => \int[k1 x]_y (k1comp k2 A (x, y)). +Lemma intker_indicE A x y : measurable A -> + intker_indic k A (x, y) = k (x, y) (xsection A y). +Proof. +move=> mA; rewrite /intker_indic(*NB:lemma?*) integral_indic//; last first. + rewrite -[X in measurable X]/(_ @^-1` _) -xsectionE. + exact: measurable_xsection. +by rewrite setIT xsectionE. +Qed. -(* (unparameterized) composition of two finite transition kernels *) -Definition kkcomp_undep d0 d1 d2 (T0 : measurableType d0) - (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} - (k1 : T0 -> {measure set T1 -> \bar R}) - (k2 : T1 -> {measure set T2 -> \bar R}) : T0 -> set (T1 * T2) -> \bar R - := kkcomp_dep k1 (fun x01 => k2 x01.2). +Lemma intker_indic_bigcup x y (A : (set (T1 * T2)) ^nat) : + trivIset [set: nat] A -> (forall n, measurable (A n)) -> + intker_indic k (\bigcup_n A n) (x, y) = + \big[+%R/0%R]_(0 <= i tA mA. +rewrite /intker_indic -(@integral_nneseries _ _ R (k (x, y)) _ measurableT + (fun i z => (\1_(A i) (y, z))%:E))//. +- by apply: eq_integral => z _ /=; rewrite indic_bigcup. +- move=> n; apply/measurable_EFinP => //; apply: measurable_indic. + rewrite -[X in measurable X]/(_ @^-1` _) -xsectionE. + exact: measurable_xsection. +- by move=> n z _; rewrite lee_fin. +Qed. -Lemma kkcomp_depE d0 d1 d2 (T0 : measurableType d0) - (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} - (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) A : - (kkcomp_dep k1 k2) ^~ A = - fun x => \int[k1 x]_y (k1comp k2 A) (x, y). -Proof. exact/funext. Qed. +End intker_indic_lemmas. (* thm 14.22 b *) -Theorem semi_sigma_additive_kkcomp_dep d0 d1 d2 (T0 : measurableType d0) +Theorem semi_sigma_additive_kproduct d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) x : - semi_sigma_additive (kkcomp_dep k1 k2 x). + semi_sigma_additive (kproduct k1 k2 x). Proof. move=> /= A mA tA mbigcup. -have /(congr1 (fun f => f x)) -> := kkcomp_depE k1 k2 (\bigcup_n A n). rewrite (_ : (fun n => _) = (fun n => - \int[k1 x]_w1 (\sum_(0 <= i < n) k1comp k2 (A i) (x, w1)))); last first. + \int[k1 x]_w1 (\sum_(0 <= i < n) intker_indic k2 (A i) (x, w1)))); last first. apply/funext => n; rewrite ge0_integral_sum//. - by move=> m; exact: measurable_prod2 (measurable_k1comp k2 (mA m)). + by move=> m; exact: measurable_prod2 (measurable_intker_indic k2 (mA m)). by move=> m y _; apply: integral_ge0 => z _; rewrite lee_fin. pose g' : nat -> T1 -> \bar R := - fun n y => \sum_(0 <= i < n) (k1comp k2 (A i) (x, y)). + fun n y => \sum_(0 <= i < n) intker_indic k2 (A i) (x, y). rewrite [X in _ --> X](_ : _ = (\int[k1 x]_y limn (g'^~ y))%E); last first. - apply: eq_integral => y _. - rewrite /g' /k1comp -(@integral_nneseries _ _ R (k2 (x, y)) _ measurableT - (fun i z => (\1_(A i) (y, z))%:E))//. - - by apply: eq_integral => z _ /=; rewrite indic_bigcup. - - move=> n; apply/measurable_EFinP => //; apply: measurable_indic. - rewrite -[X in measurable X]/(_ @^-1` _) -xsectionE. - exact: measurable_xsection. - - by move=> n z _; rewrite lee_fin. + by apply: eq_integral => y _; rewrite /g' intker_indic_bigcup. apply: (@cvg_monotone_convergence _ _ _ (k1 x) _ measurableT g') => //. -- move=> n. - apply: (@emeasurable_sum _ _ R setT _ _ - (fun i t => k1comp k2 (A i) (x, t))) => m. - exact: measurable_prod2 (measurable_k1comp k2 (mA m)). +- move=> n; apply: (@emeasurable_sum _ _ R setT _ _ + (fun i t => intker_indic k2 (A i) (x, t))) => m. + exact: measurable_prod2 (measurable_intker_indic k2 (mA m)). - move=> n y _; apply: sume_ge0 => m _. by apply: integral_ge0 => z _; rewrite lee_fin. - move=> y _ a b ab; apply: lee_sum_nneg_natr => // m _ _. by apply: integral_ge0 => z _; rewrite lee_fin. Qed. +(* thm 14.22 a *) +Theorem measurable_kproduct d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) + A : measurable A -> + measurable_fun [set: T0] (kproduct k1 k2 ^~ A). +Proof. +move=> mA; apply: measurable_kfcomp => // t. + exact: measurable_intker_indic. +by apply: integral_ge0 => y _; rewrite lee_fin. +Qed. -Section kkcomp_dep_measure. +Section kproduct_measure. Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2). -Let kkcomp_dep0 x : kkcomp_dep k1 k2 x set0 = 0. +Let kproduct0 x : kproduct k1 k2 x set0 = 0. Proof. by apply: integral0_eq => y _; apply: integral0_eq => z _; rewrite indic0. Qed. -Let kkcomp_dep_ge0 x A : (0 <= kkcomp_dep k1 k2 x A)%E. +Let kproduct_ge0 x A : (0 <= kproduct k1 k2 x A)%E. Proof. by apply: integral_ge0 => y _; apply: integral_ge0 => z _; rewrite lee_fin. Qed. -Let kkcomp_dep_additive x : semi_sigma_additive (kkcomp_dep k1 k2 x). -Proof. exact: semi_sigma_additive_kkcomp_dep. Qed. +Let kproduct_additive x : semi_sigma_additive (kproduct k1 k2 x). +Proof. exact: semi_sigma_additive_kproduct. Qed. HB.instance Definition _ (x : T0) := isMeasure.Build - (measure_prod_display (d1, d2)) (T1 * T2)%type R (kkcomp_dep k1 k2 x) - (kkcomp_dep0 x) (kkcomp_dep_ge0 x) (@kkcomp_dep_additive x). + (measure_prod_display (d1, d2)) (T1 * T2)%type R (kproduct k1 k2 x) + (kproduct0 x) (kproduct_ge0 x) (@kproduct_additive x). -Definition mkkcomp_dep := - (kkcomp_dep k1 k2 : T0 -> {measure set (T1 * T2) -> \bar R}). +Definition mkproduct := + (kproduct k1 k2 : T0 -> {measure set (T1 * T2) -> \bar R}). -End kkcomp_dep_measure. - -(* thm 14.22 a *) -Theorem measurable_kkcomp_dep d0 d1 d2 (T0 : measurableType d0) - (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} - (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) - A : measurable A -> - measurable_fun [set: T0] ((kkcomp_dep k1 k2) ^~ A). -Proof. -move=> mA; rewrite kkcomp_depE; apply: measurable_kfcomp => // t. - exact: measurable_k1comp. -by apply: integral_ge0 => y _; rewrite lee_fin. -Qed. +End kproduct_measure. HB.instance Definition _ d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) := @isKernel.Build _ _ T0 (T1 * T2)%type R - (mkkcomp_dep k1 k2) (measurable_kkcomp_dep k1 k2). + (mkproduct k1 k2) (measurable_kproduct k1 k2). -Section sigma_finite_kkcomp_dep. +Section sigma_finite_kproduct. Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2). (* thm 14.22 c: the composition of finite transition kernels is at least sigma-finite *) -Theorem sigma_finite_kkcomp_dep x : sigma_finite setT (kkcomp_dep k1 k2 x). +Theorem sigma_finite_mkproduct x : + sigma_finite [set: T1 * T2] (mkproduct k1 k2 x). Proof. pose A n := [set w1 | k2 (x, w1) setT < n%:R%:E]. have mA n : measurable (A n). @@ -1522,31 +1578,29 @@ have mA n : measurable (A n). have := @measurable_kernel _ _ _ _ R k2 _ measurableT. exact: measurable_prod2. have bigcupA : \bigcup_n A n = setT. - have fink2 xy : fin_num_fun (k2 xy) by exact: finite_finite_transition_kernel. + have fink2 xy : fin_num_fun (k2 xy) by exact: kernel_finite_transition. apply/seteqP; split => // y _. have {}fink2 := fink2 (x, y) _ measurableT. exists (Num.trunc (fine (k2 (x, y) setT))).+1 => //=. have : (0 <= fine (k2 (x, y) [set: T2]))%R by rewrite fine_ge0. move=> /Num.Theory.trunc_itv/andP[_]. by rewrite -lte_fin fineK. -have lty n : kkcomp_dep k1 k2 x (A n `*` setT) < +oo. - have fink1 : fin_num_fun (k1 x) by apply: finite_finite_transition_kernel. +have lty n : kproduct k1 k2 x (A n `*` setT) < +oo. + have fink1 : fin_num_fun (k1 x) by exact: kernel_finite_transition. apply: (@le_lt_trans _ _ (n%:R%:E * k1 x (A n))) => /=. - rewrite /kkcomp_dep. + rewrite /kproduct. rewrite [leLHS](_ : _ = \int[k1 x]_(y in A n) k2 (x, y) setT); last first. under eq_integral. move=> y _. - rewrite /k1comp(*NB:lemma?*) integral_indic//; last first. - rewrite -[X in measurable X]/(_ @^-1` _) -xsectionE. - by apply: measurable_xsection; exact: measurableX. - rewrite setIT. + rewrite intker_indicE//; last first. + exact: measurableX. over. rewrite [RHS]integral_mkcond; apply: eq_integral => y _. - rewrite patchE; case: ifPn => Anx. + rewrite patchE; case: ifPn => [/set_mem Anx|Anx]. congr (k2 _ _); apply/funext => z/=. - by apply/propext; split => // _; split => //; exact/set_mem. + by rewrite xsectionE/=; exact/propext. rewrite [X in k2 _ X = _](_ : _ = set0) ?measure0//. - rewrite -subset0// => z/= []. + rewrite -subset0// => z/=; rewrite xsectionE/= => -[]. by move: Anx; rewrite notin_setE. apply: (@le_trans _ _ (\int[k1 x]_(x in A n) n%:R%:E)). apply: ge0_le_integral => //. @@ -1561,127 +1615,115 @@ exists (fun n => A n `*` setT) => /=. by move=> n; split => //; exact: measurableX. Qed. -HB.instance Definition _ x := @SFiniteKernel_isSigmaFinite.Build d0 +HB.instance Definition _ x := @isSigmaFiniteKernel.Build d0 (measure_prod_display (d1, d2)) T0 (T1 * T2)%type R - (mkkcomp_dep k1 k2) sigma_finite_kkcomp_dep. + (mkproduct k1 k2) sigma_finite_mkproduct. -End sigma_finite_kkcomp_dep. +End sigma_finite_kproduct. -Definition kkcomp d0 d1 d2 (T0 : measurableType d0) - (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} - (k1 : (*T0 -> {measure set T1 -> \bar R}*)R.-spker T0 ~> T1) - (k2 : (*T1 -> {measure set T2 -> \bar R}*)R.-spker T1 ~> T2) - : T0 -> set T2 -> \bar R := - fun x A => \int[k1 x]_y (k2 y A). +Section kcomp_noparam. +Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) + (T2 : measurableType d2) {R : realType}. +Variable k1 : T0 -> {measure set T1 -> \bar R}. +Variable k2 : T1 -> {measure set T2 -> \bar R}. + +Definition kcomp_noparam (x : T0) (A : set T2) : \bar R := + \int[k1 x]_y (k2 y A). + +End kcomp_noparam. Section spker_snd. Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} - (k1 : R.-spker T1 ~> T2). - -(* ignore the first argument of a parameterized kernel *) -Definition kernel_snd : (T0 * T1)%type -> set T2 -> \bar R := - fun x A => k1 x.2 A. - -Let kernel_snd0 x : kernel_snd x set0 = 0. -Proof. by rewrite /kernel_snd measure0. Qed. - -Let kernel_snd_ge0 x A : (0 <= kernel_snd x A)%E. -Proof. by rewrite /kernel_snd measure_ge0. Qed. + (k : R.-spker T1 ~> T2). -Let kernel_snd_additive x : semi_sigma_additive (kernel_snd x). -Proof. by move=> F mF tF mUF; exact: measure_semi_sigma_additive. Qed. - -HB.instance Definition _ x := isMeasure.Build - d2 T2 R (kernel_snd x) - (kernel_snd0 x) (kernel_snd_ge0 x) (@kernel_snd_additive x). - -Definition mkernel_snd : (T0 * T1)%type -> {measure set T2 -> \bar R} := - fun x => k1 x.2. +(* a parameterized subprobability kernel that ignores its first argument *) +Definition kernel_snd : (T0 * T1)%type -> {measure set T2 -> \bar R} := + fun x => k x.2. Let measurable_kernel U : measurable U -> - measurable_fun [set: _] (mkernel_snd ^~ U). + measurable_fun [set: _] (kernel_snd ^~ U). Proof. -move=> mU; have /= mk1 := measurable_kernel k1 _ mU. +move=> mU; have /= mk1 := measurable_kernel k _ mU. move=> _ /= Y mY; have {}mk1 := mk1 measurableT _ mY. -have -> : ([set: T0 * T1] `&` (fun x => mkernel_snd x U) @^-1` Y) = - (setT `*` ([set: T1] `&` (k1 ^~ U) @^-1` Y)). +have -> : ([set: T0 * T1] `&` (kernel_snd ^~ U) @^-1` Y) = + (setT `*` ([set: T1] `&` (k ^~ U) @^-1` Y)). by rewrite !setTI setTX. exact: measurableX. Qed. HB.instance Definition _ := - @isKernel.Build _ _ _ _ _ mkernel_snd measurable_kernel. + @isKernel.Build _ _ _ _ _ kernel_snd measurable_kernel. -Let measure_uub : measure_fam_uub mkernel_snd. +Let measure_uub : measure_fam_uub kernel_snd. Proof. exists 2%E => /= -[x y]. -rewrite /mkernel_snd/= (@le_lt_trans _ _ 1%:E) ?lte1n//. +rewrite /kernel_snd/= (@le_lt_trans _ _ 1%:E) ?lte1n//. exact: sprob_kernel_le1. Qed. HB.instance Definition _ := - @Kernel_isFinite.Build _ _ _ _ _ mkernel_snd measure_uub. + @Kernel_isFinite.Build _ _ _ _ _ kernel_snd measure_uub. Let sprob_kernel : - (ereal_sup [set (mkernel_snd) z [set: _] | z in [set: _]] <= 1)%E. + (ereal_sup [set kernel_snd z [set: _] | z in [set: _]] <= 1)%E. Proof. -by apply: (sprob_kernelP mkernel_snd).2 => -[x y]; exact: sprob_kernel_le1. +by apply: (sprob_kernelP kernel_snd).2 => -[x y]; exact: sprob_kernel_le1. Qed. -HB.instance Definition _ := FiniteKernel_isSubProbability.Build _ _ _ _ _ - mkernel_snd sprob_kernel. +HB.instance Definition _ := isSubProbabilityKernel.Build _ _ _ _ _ + kernel_snd sprob_kernel. + +Lemma intker_indic_snd (A : set T2) x y : measurable A -> + intker_indic kernel_snd ([set: _] `*` A) (x, y) = k y A. +Proof. +move=> mA; rewrite intker_indicE//=; last exact: measurableX. +by congr (k y _); rewrite xsectionE//= setTX. +Qed. End spker_snd. (* thm 14.26 a *) -Lemma kkcompE d0 d1 d2 (T0 : measurableType d0) +Lemma kcomp_noparamE d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} - (k1 : R.-spker T0 ~> T1) (k2 : R.-spker T1 ~> T2) x A : + (k1 : T0 -> {measure set T1 -> \bar R}) + (k2 : T1 -> {measure set T2 -> \bar R}) x A : measurable A -> - kkcomp k1 k2 x A = kkcomp_undep k1 k2 x (snd @^-1` A). + kcomp_noparam k1 k2 x A = kproduct_snd k1 k2 x (snd @^-1` A). Proof. -move=> mA; rewrite /kkcomp /kkcomp_undep /kkcomp_dep. +move=> mA; rewrite /kcomp_noparam /kproduct_snd /kproduct. apply: eq_integral => y _ /=. -by rewrite /k1comp/=(*NB:lemma?*) integral_indic// setIT. +by rewrite /intker_indic/= integral_indic// setIT. Qed. -Section kkcomp_measure. +Section kcomp_noparam_measure. Context d0 d1 d2 (T0 : measurableType d0) - (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} - (k1 : R.-spker T0 ~> T1) (k2 : R.-spker T1 ~> T2). + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-spker T0 ~> T1) (k2 : R.-spker T1 ~> T2). -Let kkcomp0 x : kkcomp k1 k2 x set0 = 0. +Let kcomp_noparam0 x : kcomp_noparam k1 k2 x set0 = 0. Proof. by apply: integral0_eq => y _; rewrite measure0. Qed. -Let kkcomp_ge0 x A : 0 <= kkcomp k1 k2 x A. +Let kcomp_noparam_ge0 x A : 0 <= kcomp_noparam k1 k2 x A. Proof. by apply: integral_ge0 => y _; exact: measure_ge0. Qed. -Let kkcomp_additive x : semi_sigma_additive (kkcomp k1 k2 x). +Let kcomp_noparam_additive x : semi_sigma_additive (kcomp_noparam k1 k2 x). Proof. move=> F mF tF mUF. -rewrite kkcompE// (_ : (fun _ => _) = (fun n => - \sum_(0 <= i < n) kkcomp_undep k1 k2 x (snd @^-1` F i))); last first. - by apply/funext => n; apply: eq_bigr => k _; rewrite kkcompE. +rewrite kcomp_noparamE// (_ : (fun _ => _) = (fun n => + \sum_(0 <= i < n) kproduct_snd k1 k2 x (snd @^-1` F i))); last first. + by apply/funext => n; apply: eq_bigr => k _; rewrite kcomp_noparamE. pose F' n := [set: T1] `*` F n. -have kkcomp_bigcup : kkcomp k1 k2 x (\bigcup_n F n) = - kkcomp_dep k1 (mkernel_snd k2) x (\bigcup_n F' n). - apply: eq_integral => y _. - rewrite /k1comp /= integral_indic//; last first. - apply: bigcup_measurable => // k _. - rewrite /F' -[X in measurable X]/(_ @^-1` _) -xsectionE. - by apply: measurable_xsection; exact: measurableX. - congr (k2 y _). - apply/seteqP; split => [z [i _ Fiz] /=|z/= [[i _ Fiz]] _]. - by split => //; exists i. - by exists i => //; case: Fiz. -rewrite -kkcompE// kkcomp_bigcup. +have kcomp_noparam_bigcup : kcomp_noparam k1 k2 x (\bigcup_n F n) = + kproduct k1 (kernel_snd k2) x (\bigcup_n F' n). + by apply: eq_integral => y _; rewrite -setX_bigcupr intker_indic_snd. +rewrite -kcomp_noparamE// kcomp_noparam_bigcup. rewrite (_ : (fun n => _) = (fun n => - \sum_(0 <= i < n) kkcomp_dep k1 (mkernel_snd k2) x (F' i))); last first. + \sum_(0 <= i < n) kproduct k1 (kernel_snd k2) x (F' i))); last first. apply/funext => n; apply: eq_bigr => i _. apply: eq_integral => y _; apply: eq_integral => z _. by rewrite /F' setTX. -apply: semi_sigma_additive_kkcomp_dep => //. +apply: semi_sigma_additive_kproduct => //. - by move=> i; exact: measurableX. - apply/trivIsetP => i j _ _ ij. rewrite /F' -setXI setTI. @@ -1690,47 +1732,42 @@ apply: semi_sigma_additive_kkcomp_dep => //. - by apply: bigcup_measurable => k _; exact: measurableX. Qed. -HB.instance Definition _ x := isMeasure.Build - d2 T2 R (kkcomp k1 k2 x) (kkcomp0 x) (kkcomp_ge0 x) (@kkcomp_additive x). +HB.instance Definition _ x := isMeasure.Build d2 T2 R (kcomp_noparam k1 k2 x) + (kcomp_noparam0 x) (kcomp_noparam_ge0 x) (@kcomp_noparam_additive x). -Definition mkkcomp := (kkcomp k1 k2 : T0 -> {measure set T2 -> \bar R}). +Definition mkcomp_noparam := + (kcomp_noparam k1 k2 : T0 -> {measure set T2 -> \bar R}). Let measurable_kernel U : - measurable U -> measurable_fun [set: _] (mkkcomp ^~ U). + measurable U -> measurable_fun [set: _] (mkcomp_noparam ^~ U). Proof. move=> mU. rewrite [X in measurable_fun _ X](_ : _ = - (kkcomp_dep k1 (mkernel_snd k2))^~ ([set: T1] `*` U)); last first. + (kproduct k1 (kernel_snd k2))^~ ([set: T1] `*` U)); last first. apply/funext => x. - have := @kkcomp_depE _ _ _ T0 T1 T2 R k1 (mkernel_snd k2) (setT `*` U). - move=> /(congr1 (fun f => f x)) ->. - apply: eq_integral => y _. - rewrite /k1comp/= integral_indic//; last first. - rewrite -[X in measurable X]/(_ @^-1` _) -xsectionE. - by apply: measurable_xsection; exact: measurableX. - rewrite /mkernel_snd/= setIT; congr (k2 y _). - by apply/seteqP; split => // ? []. -by apply: measurable_kkcomp_dep; exact: measurableX. + by apply: eq_integral => y _; rewrite intker_indic_snd. +by apply: measurable_kproduct; exact: measurableX. Qed. -HB.instance Definition _ := @isKernel.Build _ _ _ _ R mkkcomp measurable_kernel. +HB.instance Definition _ := @isKernel.Build _ _ _ _ R + mkcomp_noparam measurable_kernel. -End kkcomp_measure. +End kcomp_noparam_measure. (* the composition of subprobability kernels is a subprobability kernel *) -Section subprob_kkcomp. +Section subprobability_kcomp_noparam. Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} (k1 : R.-spker T0 ~> T1) (k2 : R.-spker T1 ~> T2). (* thm 14.26 b *) -Lemma subprob_kkcomp x : (mkkcomp k1 k2 x setT <= 1)%E. +Lemma sprob_mkcomp_noparam x : (mkcomp_noparam k1 k2 x setT <= 1)%E. Proof. -rewrite /mkkcomp [leLHS]kkcompE// preimage_setT. -rewrite /kkcomp_undep /kkcomp_dep /k1comp/=. -rewrite [leLHS](_ : _ = \int[k1 x]_w1 (k2 w1 setT)); last first. +rewrite /mkcomp_noparam [leLHS]kcomp_noparamE// preimage_setT. +rewrite /kproduct_snd /kproduct. +rewrite [leLHS](_ : _ = \int[k1 x]_y k2 y setT); last first. apply: eq_integral => y _. - rewrite integral_indic//; last first. + rewrite /intker_indic integral_indic//; last first. rewrite [X in measurable X](_ : _ = ysection setT y). exact: measurable_ysection. by rewrite ysectionE. @@ -1742,10 +1779,10 @@ by move=> y _; exact: sprob_kernel_le1. Qed. Let sprob_kernel : - ereal_sup [set (mkkcomp k1 k2) x setT | x in setT] <= 1. -Proof. by apply/sprob_kernelP => x; exact: subprob_kkcomp. Qed. + ereal_sup [set (kcomp_noparam k1 k2) x setT | x in [set: T0]] <= 1. +Proof. by apply/sprob_kernelP => x; exact: sprob_mkcomp_noparam. Qed. HB.instance Definition _ := Kernel_isSubProbability.Build - _ _ _ _ R (mkkcomp k1 k2) sprob_kernel. + _ _ _ _ R (mkcomp_noparam k1 k2) sprob_kernel. -End subprob_kkcomp. +End subprobability_kcomp_noparam. From fbb88ac5c9c28dfc139c6f38f918e2ea0c9fd3d9 Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Wed, 1 Jan 2025 12:25:46 +0900 Subject: [PATCH 05/36] more doc --- theories/kernel.v | 270 +++++++++++++++++++++++---------------------- theories/measure.v | 12 +- 2 files changed, 148 insertions(+), 134 deletions(-) diff --git a/theories/kernel.v b/theories/kernel.v index 0f732fc02..8b9d428a8 100644 --- a/theories/kernel.v +++ b/theories/kernel.v @@ -10,23 +10,34 @@ From mathcomp Require Import numfun lebesgue_measure lebesgue_integral. (**md**************************************************************************) (* # Kernels *) (* *) -(* This file provides a formation of kernels, s-finite kernels, finite *) -(* kernels, subprobability kernels, and probability kernels. The main *) -(* formalized result is the fact that s-finite kernels are stable by *) -(* composition. *) -(* Reference: *) +(* This file provides a formation of kernels, s-finite kernels, *) +(* sigma-finite kernels, finite transition kernels, finite kernels, *) +(* subprobability kernels, and probability kernels, organized as a hierarchy *) +(* of mathematical structures. The main formalized results are stability by *) +(* composition for s-finite kernels [Lemma 3, Staton, ESOP 2017] and *) +(* subprobability kernels [Theorem 14.26, Klenke 2014]. *) +(* *) +(* References: *) (* - R. Affeldt, C. Cohen, A. Saito. Semantics of probabilistic programs *) (* using s-finite kernels in Coq. CPP 2023 *) +(* - S. Staton. Commutative Semantics for Probabilistic Programming. *) +(* ESOP 2017 *) +(* - A. Klenke. Probability theory: A comprehensive course. 2014 *) (* *) (* ``` *) (* R.-ker X ~> Y == kernel from X to Y where X and Y are of type *) (* measurableType *) (* The HB class is Kernel. *) +(* kseries == countable sum of kernels *) +(* It is declared as an instance of the structure *) +(* Kernel. It will also be shown to be an instance of *) +(* SFiniteKernel if the sum is over s-finite kernels. *) (* measure_fam_uub k == the kernel k is uniformly upper-bounded *) (* R.-sfker X ~> Y == s-finite kernel *) (* The HB class is SFiniteKernel. *) -(* R.-sigmafker X ~> Y == sigma-finite kernel *) -(* The HB class is SigmaFiniteKernel. *) +(* kzero == kernel defined using the mzero measure *) +(* R.-sigmafker X ~> Y == sigma-finite transition kernel *) +(* The HB class is SigmaFiniteTransitionKernel. *) (* R.-ftker X ~> Y == finite transition kernel *) (* The HB class is FiniteTransitionKernel. *) (* R.-fker X ~> Y == finite kernel *) @@ -35,15 +46,31 @@ From mathcomp Require Import numfun lebesgue_measure lebesgue_integral. (* The HB class is SubProbabilityKernel. *) (* R.-pker X ~> Y == probability kernel *) (* The HB class is ProbabilityKernel. *) -(* kseries == countable sum of kernels *) -(* It is declared as an instance of the structure *) -(* Kernel. It is also an instance of the structure *) -(* SFiniteKernel if the sum is over s-finite kernels. *) -(* kzero == kernel defined using the mzero measure *) (* kdirac mf == kernel defined by a measurable function *) (* kprobability m == kernel defined by a probability measure *) (* kadd k1 k2 == lifting of the addition of measures to kernels *) -(* l \; k == composition of kernels *) +(* knormalize f P := fun x => mnormalize (f x) P *) +(* kcomp l k == "parameterized" composition for kernels *) +(* l has type X -> {measure set Y -> \bar R} *) +(* k has type X * Y -> {measure set Z -> \bar R} *) +(* kcomp l k as type X -> set Z -> \bar R *) +(* l \; k == same as kcomp l k but equipped with the type *) +(* X -> {measure set Z -> \bar R} *) +(* kfcomp k f == composition of a "kernel" and a function *) +(* k has type X -> {measure set Y -> \bar R}. *) +(* kproduct k1 k2 == "parameterized" product of kernels *) +(* k1 has type T0 -> {measure set T1 -> \bar R}. *) +(* k2 has type T0 * T1 -> {measure set T2 -> \bar R}. *) +(* The result has type T0 -> set (T1 * T2) -> \bar R. *) +(* mkproduct k1 k2 == same as kproduct k1 k2 but equipped with the type *) +(* T0 -> {measure set (T1 * T2) -> \bar R} *) +(* kproduct_snd k1 k2 := kproduct k1 (k2 \o snd) *) +(* k2 has T1 -> {measure set T2 -> \bar R} *) +(* It "ignores" the first argument of k2. *) +(* kcomp_noparam k1 k2 := composition of kernels *) +(* k1 has type T0 -> {measure set T1 -> \bar R}. *) +(* k2 has type T1 -> {measure set T2 -> \bar R}. *) +(* The return type is T0 -> set (T1 * T2) -> \bar R *) (* ``` *) (* *) (******************************************************************************) @@ -226,21 +253,21 @@ Proof. by exists 1%R => /= t; rewrite /mzero/=. Qed. End kzero. (* interface for sigma-finite kernel *) -HB.mixin Record isSigmaFiniteKernel +HB.mixin Record isSigmaFiniteTransitionKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : X -> {measure set Y -> \bar R}) := { kernel_sigma_finite : forall x, sigma_finite [set: Y] (k x) }. #[short(type=sigma_finite_kernel)] -HB.structure Definition SigmaFiniteKernel +HB.structure Definition SigmaFiniteTransitionKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) := { k of @Kernel _ _ _ _ _ k & - isSigmaFiniteKernel _ _ X Y R k }. + isSigmaFiniteTransitionKernel _ _ X Y R k }. Notation "R .-sigmafker X ~> Y" := (sigma_finite_kernel X%type Y R). (* interface for finite transition kernel *) -HB.mixin Record isFiniteTransitionKernel +HB.mixin Record isFiniteTransition d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : X -> {measure set Y -> \bar R}) := { kernel_finite_transition : forall x, fin_num_fun (k x) }. @@ -249,7 +276,7 @@ HB.mixin Record isFiniteTransitionKernel HB.structure Definition FiniteTransitionKernel d d' (X : measurableType d) (Y : measurableType d') (R : realType) := { k of @SFiniteKernel _ _ _ _ _ k & - isFiniteTransitionKernel _ _ X Y R k }. + isFiniteTransition _ _ X Y R k }. Notation "R .-ftker X ~> Y" := (finite_transition_kernel X%type Y R). @@ -301,16 +328,15 @@ Qed. HB.instance Definition _ := @isSFiniteKernel_subdef.Build d d' X Y R k sfinite_finite. -(*Let kernel_sigma_finite x : sigma_finite setT (k x). +Let kernel_sigma_finite x : sigma_finite setT (k x). Proof. apply: fin_num_fun_sigma_finite; first by rewrite measure0. apply: lty_fin_num_fun. -have [r kr] := measure_uub. -by rewrite (lt_trans (kr x)) ?ltry. +by have [r kr] := measure_uub; rewrite (lt_trans (kr x)) ?ltry. Qed. HB.instance Definition _ := - @isSigmaFiniteKernel.Build d d' X Y R k kernel_sigma_finite.*) + @isSigmaFiniteTransitionKernel.Build d d' X Y R k kernel_sigma_finite. Let finite_transition_finite : forall x, fin_num_fun (k x). Proof. @@ -322,7 +348,7 @@ by rewrite (lt_trans (kr x)) ?ltry. Qed. HB.instance Definition _ := - @isFiniteTransitionKernel.Build d d' X Y R k finite_transition_finite. + @isFiniteTransition.Build d d' X Y R k finite_transition_finite. HB.instance Definition _ := @isMeasureFamUub.Build d d' X Y R k measure_uub. @@ -455,6 +481,11 @@ split => [+ x|k1]; last by apply: ub_ereal_sup => _ /= [z _ <-]; exact: k1. by apply/le_trans/ereal_sup_ubound => /=; exists x. Qed. +Lemma sprob_kernel_le1 d d' (X : measurableType d) + (Y : measurableType d') (R : realType) (k : R.-spker X ~> Y) x : + k x [set: Y] <= 1. +Proof. by apply: (sprob_kernelP (fun x A => k x A)).1; exact: sprob_kernel. Qed. + HB.factory Record Kernel_isSubProbability d d' (X : measurableType d) (Y : measurableType d') (R : realType) (k : X -> {measure set Y -> \bar R}) of isKernel _ _ X Y R k := { @@ -789,6 +820,7 @@ Qed. HB.instance Definition _ := @isKernel.Build _ _ _ _ _ kadd measurable_fun_kadd. + End kadd. Section sfkadd. @@ -815,6 +847,7 @@ Qed. HB.instance Definition _ t := isSFiniteKernel_subdef.Build _ _ _ _ R (kadd k1 k2) sfinite_kadd. + End sfkadd. Section fkadd. @@ -831,6 +864,7 @@ Qed. HB.instance Definition _ t := Kernel_isFinite.Build _ _ _ _ R (kadd k1 k2) kadd_finite_uub. + End fkadd. Section knormalize. @@ -910,9 +944,8 @@ apply: measurable_fun_if => //. + by apply: measurableT_comp => //; exact/measurable_funS/measurable_kernel. Qed. -(* parameterized composition of kernels - return type: X -> set Z -> \bar R, - as used in staton2017esop *) +(* "parameterized composition" of kernels [Lemma 3, Staton ESOP 2017] + return type: X -> set Z -> \bar R *) Section kcomp_def. Context d1 d2 d3 (X : measurableType d1) (Y : measurableType d2) (Z : measurableType d3) (R : realType). @@ -923,6 +956,7 @@ Definition kcomp x (U : set Z) := \int[l x]_y k (x, y) U. End kcomp_def. +(* [Lemma 3, Staton 2017 ESOP] (1/4) *) Section kcomp_is_measure. Context d1 d2 d3 (X : measurableType d1) (Y : measurableType d2) (Z : measurableType d3) (R : realType). @@ -956,6 +990,7 @@ End kcomp_is_measure. Notation "l \; k" := (mkcomp l k) : ereal_scope. +(* [Lemma 3, Staton 2017 ESOP] (2/4) *) Module KCOMP_FINITE_KERNEL. Section kcomp_finite_kernel_kernel. @@ -998,6 +1033,7 @@ HB.instance Definition _ := End kcomp_finite_kernel_finite. End KCOMP_FINITE_KERNEL. +(* [Lemma 3, Staton 2017 ESOP] (3/4) *) Section kcomp_sfinite_kernel. Context d d' d3 (X : measurableType d) (Y : measurableType d') (Z : measurableType d3) (R : realType). @@ -1183,6 +1219,7 @@ rewrite integral0_eq ?mule0; last first. by rewrite integral0_eq// => y _; rewrite preimage_nnfun0// measure0 mule0. Qed. +(* [Lemma 3, Staton 2017 ESOP] (4/4) *) Lemma integral_kcomp x f : (forall z, 0 <= f z) -> measurable_fun [set: Z] f -> \int[kcomp l k x]_z f z = \int[l x]_y (\int[k (x, y)]_z f z). Proof. @@ -1219,34 +1256,10 @@ Qed. End integral_kcomp. -(* composition of finite transition kernels, following Klenke *) - -(* lem 14.13 *) -Section measurable_fun_prod. -Context d1 d2 d3 (T1 : measurableType d1) (T2 : measurableType d2) - (T3 : measurableType d3). - -Lemma measurable_prod1 (f : T1 * T2 -> T3) (y : T2) : - measurable_fun setT f -> measurable_fun setT (fun x => f (x, y)). -Proof. by move=> mf; exact: measurableT_comp. Qed. - -Lemma measurable_prod2 (f : T1 * T2 -> T3) (x : T1) : - measurable_fun setT f -> measurable_fun setT (fun y => f (x, y)). -Proof. by move=> mf; exact: measurableT_comp. Qed. - -End measurable_fun_prod. - -Lemma sprob_kernel_le1 d d' (X : measurableType d) - (Y : measurableType d') (R : realType) (k : R.-spker X ~> Y) x : - k x [set: Y] <= 1. -Proof. -by apply: (sprob_kernelP (fun x A => k x A)).1; exact: sprob_kernel. -Qed. - -(* composition of a finite transition kernel and a function, as in def 14.20 *) -Definition kfcomp d1 d2 - (T1 : measurableType d1) (T2 : measurableType d2) (R : realType) - (k : R.-ftker T1 ~> T2) (f : T1 * T2 -> \bar R) : T1 -> \bar R := +(* [Definition 14.20, Klenke 2014 ]*) +Definition kfcomp d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) + (R : realType) (k : T1 -> {measure set T2 -> \bar R}) + (f : T1 * T2 -> \bar R) : T1 -> \bar R := fun x => \int[k x]_y f (x, y). Section measurable_kfcomp. @@ -1269,11 +1282,11 @@ Qed. Import HBNNSimple. -(* lem 14.20 *) +(* [Lemma 14.20, Klenke 2014] *) Lemma measurable_kfcomp d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) (R : realType) (k : R.-ftker T1 ~> T2) - (f : T1 * T2 -> \bar R) : measurable_fun [set: (T1 * T2)%type] f -> - (forall t, (0 <= f t)%E) -> + (f : T1 * T2 -> \bar R) : measurable_fun [set: T1 * T2] f -> + (forall t, 0 <= f t) -> measurable_fun [set: T1] (kfcomp k f). Proof. move=> mf f0. @@ -1289,15 +1302,13 @@ have IgE (A1 : set T1) (A2 : set T2) x : measurable A1 -> measurable A2 -> by rewrite xsectionE. by apply: measurable_xsection; exact: measurableX. have [xA1|xA1] := boolP (x \in A1). - rewrite indicE xA1 mul1e; congr (k _ _). - by rewrite setIT/=; apply/seteqP; split => [y []//|y]; move/set_mem: xA1. - rewrite indicE (negbTE xA1) mul0e [X in k _ X = _](_ : _ = set0)//. - by rewrite setIT -subset0 => y -[/= /mem_set]; rewrite (negbTE xA1). -suff: measurable_fun [set: T1] (I f) by []. + by rewrite indicE xA1 mul1e setIT -[X in _ _ X = _]xsectionE in_xsectionX. + rewrite indicE (negbTE xA1) mul0e setIT. + by rewrite -[X in _ _ X = _]xsectionE notin_xsectionX. +rewrite -[X in measurable_fun _ X]/(I f). pose f_ := nnsfun_approx measurableT mf. have If_cvg x : I (EFin \o f_ n) x @[n --> \oo] --> I f x. - rewrite /I. - pose g' := fun n y => (EFin \o f_ n) (x, y). + pose g' n y := (EFin \o f_ n) (x, y). rewrite [X in _ --> X](_ : _ = \int[k x]_y (fun t => limn (g' ^~ t)) y); last first. apply: eq_integral => y _. @@ -1305,8 +1316,7 @@ have If_cvg x : I (EFin \o f_ n) x @[n --> \oo] --> I f x. apply: cvg_monotone_convergence => //. - by move=> n; apply/measurable_EFinP; exact: measurableT_comp. - by move=> n y _; rewrite /= lee_fin. - - move=> y _ a b ab /=; rewrite lee_fin. - by have /lefP/(_ (x, y)) := nd_nnsfun_approx measurableT mf ab. + - by move=> y _ a b ab/=; rewrite lee_fin; exact/lefP/nd_nnsfun_approx. apply: (emeasurable_fun_cvg (fun n => I (EFin \o f_ n))). move=> m. pose D := [set A | measurable A /\ measurable_fun setT (I (EFin \o \1_A))]. @@ -1423,8 +1433,7 @@ Qed. End measurable_kfcomp. -(* product of kernels - as in klenke2014 +(* [Theorem 14.22, Klenke 2014] return type : T0 -> set (T1 * T2) -> \bar R *) Section kproduct_def. Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) @@ -1432,7 +1441,7 @@ Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) Variable k1 : T0 -> {measure set T1 -> \bar R}. Variable k2 : T0 * T1 -> {measure set T2 -> \bar R}. -Definition intker_indic (k : T0 * T1 -> {measure set T2 -> \bar R}) A := +Local Definition intker_indic (k : T0 * T1 -> {measure set T2 -> \bar R}) A := fun xy => \int[k xy]_z (\1_A (xy.2, z))%:E. Definition kproduct x (A : set (T1 * T2)) := @@ -1440,19 +1449,12 @@ Definition kproduct x (A : set (T1 * T2)) := End kproduct_def. -(* (unparameterized) product *) -Definition kproduct_snd d0 d1 d2 (T0 : measurableType d0) - (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} - (k1 : T0 -> {measure set T1 -> \bar R}) - (k2 : T1 -> {measure set T2 -> \bar R}) : T0 -> set (T1 * T2) -> \bar R := - kproduct k1 (k2 \o snd). - Section intker_indic_lemmas. Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType}. Variable k : R.-ftker T0 * T1 ~> T2. -Lemma measurable_intker_indic A : measurable A -> +Local Lemma measurable_intker_indic A : measurable A -> measurable_fun [set: T0 * T1] (intker_indic k A). Proof. move=> mA; apply: (@measurable_kfcomp _ _ (T0 * T1)%type T2 R k @@ -1465,7 +1467,7 @@ move=> mA; apply: (@measurable_kfcomp _ _ (T0 * T1)%type T2 R k - by move=> t; rewrite lee_fin. Qed. -Lemma intker_indicE A x y : measurable A -> +Local Lemma intker_indicE A x y : measurable A -> intker_indic k A (x, y) = k (x, y) (xsection A y). Proof. move=> mA; rewrite /intker_indic(*NB:lemma?*) integral_indic//; last first. @@ -1474,7 +1476,7 @@ move=> mA; rewrite /intker_indic(*NB:lemma?*) integral_indic//; last first. by rewrite setIT xsectionE. Qed. -Lemma intker_indic_bigcup x y (A : (set (T1 * T2)) ^nat) : +Local Lemma intker_indic_bigcup x y (A : (set (T1 * T2)) ^nat) : trivIset [set: nat] A -> (forall n, measurable (A n)) -> intker_indic k (\bigcup_n A n) (x, y) = \big[+%R/0%R]_(0 <= i {measure set T1 -> \bar R}. +Variable k2 : T1 -> {measure set T2 -> \bar R}. + +Definition kproduct_snd : T0 -> set (T1 * T2) -> \bar R := + kproduct k1 (k2 \o snd). + +End kproduct_snd_def. + +(* [Theorem 14.22, Klenke 2014] (1/3) *) +Theorem measurable_kproduct d0 d1 d2 (T0 : measurableType d0) + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) A : + measurable A -> measurable_fun [set: T0] (kproduct k1 k2 ^~ A). +Proof. +move=> mA; apply: measurable_kfcomp => // t. + exact: measurable_intker_indic. +by apply: integral_ge0 => y _; rewrite lee_fin. +Qed. + +(* [Theorem 14.22, Klenke 2014] (2/3) *) Theorem semi_sigma_additive_kproduct d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) x : @@ -1499,39 +1523,26 @@ Theorem semi_sigma_additive_kproduct d0 d1 d2 (T0 : measurableType d0) Proof. move=> /= A mA tA mbigcup. rewrite (_ : (fun n => _) = (fun n => - \int[k1 x]_w1 (\sum_(0 <= i < n) intker_indic k2 (A i) (x, w1)))); last first. + \int[k1 x]_y (\sum_(0 <= i < n) intker_indic k2 (A i) (x, y)))); last first. apply/funext => n; rewrite ge0_integral_sum//. - by move=> m; exact: measurable_prod2 (measurable_intker_indic k2 (mA m)). - by move=> m y _; apply: integral_ge0 => z _; rewrite lee_fin. -pose g' : nat -> T1 -> \bar R := - fun n y => \sum_(0 <= i < n) intker_indic k2 (A i) (x, y). -rewrite [X in _ --> X](_ : _ = (\int[k1 x]_y limn (g'^~ y))%E); last first. - by apply: eq_integral => y _; rewrite /g' intker_indic_bigcup. -apply: (@cvg_monotone_convergence _ _ _ (k1 x) _ measurableT g') => //. + by move=> m; exact: measurable_prod2 (measurable_intker_indic k2 _). + by move=> m y _; rewrite intker_indicE. +pose g n y := \sum_(0 <= i < n) intker_indic k2 (A i) (x, y). +rewrite [X in _ --> X](_ : _ = \int[k1 x]_y limn (g ^~ y)); last first. + by apply: eq_integral => y _; rewrite /g intker_indic_bigcup. +apply: (@cvg_monotone_convergence _ _ _ (k1 x) _ measurableT g) => //. - move=> n; apply: (@emeasurable_sum _ _ R setT _ _ (fun i t => intker_indic k2 (A i) (x, t))) => m. exact: measurable_prod2 (measurable_intker_indic k2 (mA m)). -- move=> n y _; apply: sume_ge0 => m _. - by apply: integral_ge0 => z _; rewrite lee_fin. +- by move=> n y _; apply: sume_ge0 => m _; rewrite intker_indicE. - move=> y _ a b ab; apply: lee_sum_nneg_natr => // m _ _. - by apply: integral_ge0 => z _; rewrite lee_fin. -Qed. -(* thm 14.22 a *) -Theorem measurable_kproduct d0 d1 d2 (T0 : measurableType d0) - (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} - (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) - A : measurable A -> - measurable_fun [set: T0] (kproduct k1 k2 ^~ A). -Proof. -move=> mA; apply: measurable_kfcomp => // t. - exact: measurable_intker_indic. -by apply: integral_ge0 => y _; rewrite lee_fin. + by rewrite intker_indicE. Qed. Section kproduct_measure. Context d0 d1 d2 (T0 : measurableType d0) - (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} - (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2). + (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} + (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2). Let kproduct0 x : kproduct k1 k2 x set0 = 0. Proof. @@ -1546,7 +1557,7 @@ Qed. Let kproduct_additive x : semi_sigma_additive (kproduct k1 k2 x). Proof. exact: semi_sigma_additive_kproduct. Qed. -HB.instance Definition _ (x : T0) := isMeasure.Build +HB.instance Definition _ x := isMeasure.Build (measure_prod_display (d1, d2)) (T1 * T2)%type R (kproduct k1 k2 x) (kproduct0 x) (kproduct_ge0 x) (@kproduct_additive x). @@ -1555,6 +1566,8 @@ Definition mkproduct := End kproduct_measure. +(* [Theorem 14.22, Klenke 2014] (3/3): the composition of finite transition + kernels is sigma-finite *) HB.instance Definition _ d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) := @@ -1566,8 +1579,6 @@ Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2). -(* thm 14.22 c: the composition of finite transition kernels is - at least sigma-finite *) Theorem sigma_finite_mkproduct x : sigma_finite [set: T1 * T2] (mkproduct k1 k2 x). Proof. @@ -1583,25 +1594,16 @@ have bigcupA : \bigcup_n A n = setT. have {}fink2 := fink2 (x, y) _ measurableT. exists (Num.trunc (fine (k2 (x, y) setT))).+1 => //=. have : (0 <= fine (k2 (x, y) [set: T2]))%R by rewrite fine_ge0. - move=> /Num.Theory.trunc_itv/andP[_]. - by rewrite -lte_fin fineK. + by move=> /Num.Theory.trunc_itv/andP[_]; rewrite -lte_fin fineK. have lty n : kproduct k1 k2 x (A n `*` setT) < +oo. have fink1 : fin_num_fun (k1 x) by exact: kernel_finite_transition. apply: (@le_lt_trans _ _ (n%:R%:E * k1 x (A n))) => /=. - rewrite /kproduct. rewrite [leLHS](_ : _ = \int[k1 x]_(y in A n) k2 (x, y) setT); last first. - under eq_integral. - move=> y _. - rewrite intker_indicE//; last first. - exact: measurableX. - over. - rewrite [RHS]integral_mkcond; apply: eq_integral => y _. - rewrite patchE; case: ifPn => [/set_mem Anx|Anx]. - congr (k2 _ _); apply/funext => z/=. - by rewrite xsectionE/=; exact/propext. - rewrite [X in k2 _ X = _](_ : _ = set0) ?measure0//. - rewrite -subset0// => z/=; rewrite xsectionE/= => -[]. - by move: Anx; rewrite notin_setE. + rewrite /kproduct [RHS]integral_mkcond; apply: eq_integral => y _. + rewrite intker_indicE//; last exact: measurableX. + rewrite patchE; case: ifPn => [Anx|Anx]. + by rewrite in_xsectionX. + by rewrite notin_xsectionX// measure0. apply: (@le_trans _ _ (\int[k1 x]_(x in A n) n%:R%:E)). apply: ge0_le_integral => //. - apply: measurable_funTS. @@ -1615,13 +1617,15 @@ exists (fun n => A n `*` setT) => /=. by move=> n; split => //; exact: measurableX. Qed. -HB.instance Definition _ x := @isSigmaFiniteKernel.Build d0 +HB.instance Definition _ x := @isSigmaFiniteTransitionKernel.Build d0 (measure_prod_display (d1, d2)) T0 (T1 * T2)%type R (mkproduct k1 k2) sigma_finite_mkproduct. End sigma_finite_kproduct. -Section kcomp_noparam. +(* [Definition 14.25, Klenke 2014] + return type: T0 -> set T2 -> \bar R *) +Section kcomp_noparam_def. Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType}. Variable k1 : T0 -> {measure set T1 -> \bar R}. @@ -1630,24 +1634,24 @@ Variable k2 : T1 -> {measure set T2 -> \bar R}. Definition kcomp_noparam (x : T0) (A : set T2) : \bar R := \int[k1 x]_y (k2 y A). -End kcomp_noparam. +End kcomp_noparam_def. +(* a parameterized subprobability kernel that ignores its first argument *) Section spker_snd. Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} (k : R.-spker T1 ~> T2). -(* a parameterized subprobability kernel that ignores its first argument *) -Definition kernel_snd : (T0 * T1)%type -> {measure set T2 -> \bar R} := - fun x => k x.2. +Local Definition kernel_snd : (T0 * T1)%type -> {measure set T2 -> \bar R} := + k \o snd. Let measurable_kernel U : measurable U -> measurable_fun [set: _] (kernel_snd ^~ U). Proof. move=> mU; have /= mk1 := measurable_kernel k _ mU. move=> _ /= Y mY; have {}mk1 := mk1 measurableT _ mY. -have -> : ([set: T0 * T1] `&` (kernel_snd ^~ U) @^-1` Y) = - (setT `*` ([set: T1] `&` (k ^~ U) @^-1` Y)). +have -> : [set: T0 * T1] `&` (kernel_snd ^~ U) @^-1` Y = + setT `*` ([set: T1] `&` (k ^~ U) @^-1` Y). by rewrite !setTI setTX. exact: measurableX. Qed. @@ -1674,7 +1678,7 @@ Qed. HB.instance Definition _ := isSubProbabilityKernel.Build _ _ _ _ _ kernel_snd sprob_kernel. -Lemma intker_indic_snd (A : set T2) x y : measurable A -> +Local Lemma intker_indic_snd (A : set T2) x y : measurable A -> intker_indic kernel_snd ([set: _] `*` A) (x, y) = k y A. Proof. move=> mA; rewrite intker_indicE//=; last exact: measurableX. @@ -1683,7 +1687,7 @@ Qed. End spker_snd. -(* thm 14.26 a *) +(* [Theorem 14.26, Klenke 2014] (1/2) *) Lemma kcomp_noparamE d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} (k1 : T0 -> {measure set T1 -> \bar R}) @@ -1760,7 +1764,7 @@ Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} (k1 : R.-spker T0 ~> T1) (k2 : R.-spker T1 ~> T2). -(* thm 14.26 b *) +(* [Theorem 14.26, Klenke 2014] (2/2) *) Lemma sprob_mkcomp_noparam x : (mkcomp_noparam k1 k2 x setT <= 1)%E. Proof. rewrite /mkcomp_noparam [leLHS]kcomp_noparamE// preimage_setT. diff --git a/theories/measure.v b/theories/measure.v index 6f688297a..0a3b817a0 100644 --- a/theories/measure.v +++ b/theories/measure.v @@ -5252,8 +5252,10 @@ End partial_measurable_fun. #[global] Hint Extern 0 (measurable_fun _ (pair^~ _)) => solve [apply: measurable_pair2] : core. +(* [Lemma 14.13, Klenke 2014] *) Section measurable_section. -Context d1 d2 (T1 : measurableType d1) (T2 : measurableType d2). +Context d1 d2 d3 (T1 : measurableType d1) (T2 : measurableType d2) + (T3 : measurableType d3). Lemma measurable_xsection (A : set (T1 * T2)) (x : T1) : measurable A -> measurable (xsection A x). @@ -5271,6 +5273,14 @@ have mi : measurable_fun setT i by exact: measurable_pair2. by rewrite ysectionE -[X in measurable X]setTI; exact: mi. Qed. +Lemma measurable_prod1 (f : T1 * T2 -> T3) (y : T2) : + measurable_fun setT f -> measurable_fun setT (fun x => f (x, y)). +Proof. by move=> mf; exact: measurableT_comp. Qed. + +Lemma measurable_prod2 (f : T1 * T2 -> T3) (x : T1) : + measurable_fun setT f -> measurable_fun setT (fun y => f (x, y)). +Proof. by move=> mf; exact: measurableT_comp. Qed. + End measurable_section. Section absolute_continuity. From a982f49213fb747090d1e4a02a4eb2febdbdcc40 Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Wed, 12 Apr 2023 17:31:00 +0900 Subject: [PATCH 06/36] probabilistic language using mca's kernels Co-authored-by: Cyril Cohen Co-authored-by: @AyumuSaito --- _CoqProject | 2 + theories/Make | 2 + theories/prob_lang.v | 1003 ++++++++++++++++++++++++++++++++++++++ theories/prob_lang_wip.v | 148 ++++++ 4 files changed, 1155 insertions(+) create mode 100644 theories/prob_lang.v create mode 100644 theories/prob_lang_wip.v diff --git a/_CoqProject b/_CoqProject index a8b38635c..bf2b97069 100644 --- a/_CoqProject +++ b/_CoqProject @@ -93,3 +93,5 @@ theories/pi_irrational.v theories/showcase/summability.v analysis_stdlib/Rstruct_topology.v analysis_stdlib/showcase/uniform_bigO.v +theories/prob_lang.v +theories/prob_lang_wip.v diff --git a/theories/Make b/theories/Make index 35f6699fb..4167837f0 100644 --- a/theories/Make +++ b/theories/Make @@ -59,3 +59,5 @@ kernel.v pi_irrational.v all_analysis.v showcase/summability.v +prob_lang.v +prob_lang_wip.v diff --git a/theories/prob_lang.v b/theories/prob_lang.v new file mode 100644 index 000000000..bffc1621c --- /dev/null +++ b/theories/prob_lang.v @@ -0,0 +1,1003 @@ +(* mathcomp analysis (c) 2022 Inria and AIST. License: CeCILL-C. *) +From HB Require Import structures. +From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval finmap. +From mathcomp Require Import rat. +From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. +From mathcomp.classical Require Import functions cardinality fsbigop. +Require Import reals ereal signed topology normedtype sequences esum measure. +Require Import lebesgue_measure numfun lebesgue_integral exp kernel. + +(******************************************************************************) +(* Semantics of a probabilistic programming language using s-finite kernels *) +(* *) +(* bernoulli r1 == Bernoulli probability with r1 a proof that *) +(* r : {nonneg R} is smaller than 1 *) +(* *) +(* sample P == sample according to the probability P *) +(* letin l k == execute l, augment the context, and execute k *) +(* ret mf == access the context with f and return the result *) +(* score mf == observe t from d, where f is the density of d and *) +(* t occurs in f *) +(* e.g., score (r e^(-r * t)) = observe t from exp(r) *) +(* pnormalize k P == normalize the kernel k into a probability kernel, *) +(* P is a default probability in case normalization is *) +(* not possible *) +(* ite mf k1 k2 == access the context with the boolean function f and *) +(* behaves as k1 or k2 according to the result *) +(* *) +(* poisson == Poisson distribution function *) +(* exp_density == density function for exponential distribution *) +(* *) +(******************************************************************************) + +Set Implicit Arguments. +Unset Strict Implicit. +Unset Printing Implicit Defensive. +Import Order.TTheory GRing.Theory Num.Def Num.ExtraDef Num.Theory. +Import numFieldTopology.Exports. + +Local Open Scope classical_set_scope. +Local Open Scope ring_scope. +Local Open Scope ereal_scope. + +(* TODO: PR *) +Lemma onem1' (R : numDomainType) (p : R) : (p + `1- p = 1)%R. +Proof. by rewrite /onem addrCA subrr addr0. Qed. + +Lemma onem_nonneg_proof (R : numDomainType) (p : {nonneg R}) : + (p%:num <= 1 -> 0 <= `1-(p%:num))%R. +Proof. by rewrite /onem/= subr_ge0. Qed. + +Definition onem_nonneg (R : numDomainType) (p : {nonneg R}) + (p1 : (p%:num <= 1)%R) := + NngNum (onem_nonneg_proof p1). +(* /TODO: PR *) + +Section bernoulli. +Variables (R : realType) (p : {nonneg R}) (p1 : (p%:num <= 1)%R). +Local Open Scope ring_scope. + +Definition bernoulli : set _ -> \bar R := + measure_add + [the measure _ _ of mscale p [the measure _ _ of dirac true]] + [the measure _ _ of mscale (onem_nonneg p1) [the measure _ _ of dirac false]]. + +HB.instance Definition _ := Measure.on bernoulli. + +Local Close Scope ring_scope. + +Let bernoulli_setT : bernoulli [set: _] = 1. +Proof. +rewrite /bernoulli/= /measure_add/= /msum 2!big_ord_recr/= big_ord0 add0e/=. +by rewrite /mscale/= !diracT !mule1 -EFinD onem1'. +Qed. + +HB.instance Definition _ := @Measure_isProbability.Build _ _ R bernoulli bernoulli_setT. + +End bernoulli. + +Section mscore. +Context d (T : measurableType d) (R : realType). +Variable f : T -> R. + +Definition mscore t : {measure set _ -> \bar R} := + let p := NngNum (normr_ge0 (f t)) in + [the measure _ _ of mscale p [the measure _ _ of dirac tt]]. + +Lemma mscoreE t U : mscore t U = if U == set0 then 0 else `| (f t)%:E |. +Proof. +rewrite /mscore/= /mscale/=; have [->|->] := set_unit U. + by rewrite eqxx dirac0 mule0. +by rewrite diracT mule1 (negbTE setT0). +Qed. + +Lemma measurable_fun_mscore U : measurable_fun setT f -> + measurable_fun setT (mscore ^~ U). +Proof. +move=> mr; under eq_fun do rewrite mscoreE/=. +have [U0|U0] := eqVneq U set0; first exact: measurable_cst. +by apply: measurableT_comp => //; exact: measurableT_comp. +Qed. + +End mscore. + +(* decomposition of score into finite kernels *) +Module SCORE. +Section score. +Context d (T : measurableType d) (R : realType). +Variable f : T -> R. + +Definition k (mf : measurable_fun setT f) i t U := + if i%:R%:E <= mscore f t U < i.+1%:R%:E then + mscore f t U + else + 0. + +Hypothesis mf : measurable_fun setT f. + +Lemma k0 i t : k mf i t (set0 : set unit) = 0 :> \bar R. +Proof. by rewrite /k measure0; case: ifP. Qed. + +Lemma k_ge0 i t B : 0 <= k mf i t B. +Proof. by rewrite /k; case: ifP. Qed. + +Lemma k_sigma_additive i t : semi_sigma_additive (k mf i t). +Proof. +move=> /= F mF tF mUF; rewrite /k /=. +have [F0|UF0] := eqVneq (\bigcup_n F n) set0. + rewrite F0 measure0 (_ : (fun _ => _) = cst 0). + by case: ifPn => _; exact: cvg_cst. + apply/funext => k; rewrite big1// => n _. + by move: F0 => /bigcup0P -> //; rewrite measure0; case: ifPn. +move: (UF0) => /eqP/bigcup0P/existsNP[m /not_implyP[_ /eqP Fm0]]. +rewrite [in X in _ --> X]mscoreE (negbTE UF0). +rewrite -(cvg_shiftn m.+1)/=. +case: ifPn => ir. + rewrite (_ : (fun _ => _) = cst `|(f t)%:E|); first exact: cvg_cst. + apply/funext => n. + rewrite big_mkord (bigD1 (widen_ord (leq_addl n _) (Ordinal (ltnSn m))))//=. + rewrite [in X in X + _]mscoreE (negbTE Fm0) ir big1 ?adde0// => /= j jk. + rewrite mscoreE; have /eqP -> : F j == set0. + have [/eqP//|Fjtt] := set_unit (F j). + move/trivIsetP : tF => /(_ j m Logic.I Logic.I jk). + by rewrite Fjtt setTI => /eqP; rewrite (negbTE Fm0). + by rewrite eqxx; case: ifP. +rewrite (_ : (fun _ => _) = cst 0); first exact: cvg_cst. +apply/funext => n. +rewrite big_mkord (bigD1 (widen_ord (leq_addl n _) (Ordinal (ltnSn m))))//=. +rewrite [in X in if X then _ else _]mscoreE (negbTE Fm0) (negbTE ir) add0e. +rewrite big1//= => j jm; rewrite mscoreE; have /eqP -> : F j == set0. + have [/eqP//|Fjtt] := set_unit (F j). + move/trivIsetP : tF => /(_ j m Logic.I Logic.I jm). + by rewrite Fjtt setTI => /eqP; rewrite (negbTE Fm0). +by rewrite eqxx; case: ifP. +Qed. + +HB.instance Definition _ i t := isMeasure.Build _ _ _ + (k mf i t) (k0 i t) (k_ge0 i t) (@k_sigma_additive i t). + +Lemma measurable_fun_k i U : measurable U -> measurable_fun setT (k mf i ^~ U). +Proof. +move=> /= mU; rewrite /k /= (_ : (fun x => _) = + (fun x => if i%:R%:E <= x < i.+1%:R%:E then x else 0) \o (mscore f ^~ U)) //. +apply: measurableT_comp => /=; last exact/measurable_fun_mscore. +rewrite (_ : (fun x => _) = (fun x => x * + (\1_(`[i%:R%:E, i.+1%:R%:E [%classic : set _) x)%:E)); last first. + apply/funext => x; case: ifPn => ix; first by rewrite indicE/= mem_set ?mule1. + by rewrite indicE/= memNset ?mule0// /= in_itv/=; exact/negP. +apply: emeasurable_funM => //=; apply/EFin_measurable_fun. +by rewrite (_ : \1__ = mindic R (emeasurable_itv `[(i%:R)%:E, (i.+1%:R)%:E[)). +Qed. + +Definition mk i t := [the measure _ _ of k mf i t]. + +HB.instance Definition _ i := + isKernel.Build _ _ _ _ _ (mk i) (measurable_fun_k i). + +Lemma mk_uub i : measure_fam_uub (mk i). +Proof. +exists i.+1%:R => /= t; rewrite /k mscoreE setT_unit. +by case: ifPn => //; case: ifPn => // _ /andP[]. +Qed. + +HB.instance Definition _ i := + Kernel_isFinite.Build _ _ _ _ _ (mk i) (mk_uub i). + +End score. +End SCORE. + +Section kscore. +Context d (T : measurableType d) (R : realType). +Variable f : T -> R. + +Definition kscore (mf : measurable_fun setT f) + : T -> {measure set _ -> \bar R} := + mscore f. + +Variable mf : measurable_fun setT f. + +Let measurable_fun_kscore U : measurable U -> + measurable_fun setT (kscore mf ^~ U). +Proof. by move=> /= _; exact: measurable_fun_mscore. Qed. + +HB.instance Definition _ := isKernel.Build _ _ T _ R + (kscore mf) measurable_fun_kscore. + +Import SCORE. + +Let sfinite_kscore : exists k : (R.-fker T ~> _)^nat, + forall x U, measurable U -> + kscore mf x U = mseries (k ^~ x) 0 U. +Proof. +rewrite /=; exists (fun i => [the R.-fker _ ~> _ of mk mf i]) => /= t U mU. +rewrite /mseries /kscore/= mscoreE; case: ifPn => [/eqP U0|U0]. + by apply/esym/eseries0 => i _; rewrite U0 measure0. +rewrite /mk /= /k /= mscoreE (negbTE U0). +apply/esym/cvg_lim => //. +rewrite -(cvg_shiftn `|floor (fine `|(f t)%:E|)|%N.+1)/=. +rewrite (_ : (fun _ => _) = cst `|(f t)%:E|); first exact: cvg_cst. +apply/funext => n. +pose floor_f := widen_ord (leq_addl n `|floor `|f t| |.+1) + (Ordinal (ltnSn `|floor `|f t| |)). +rewrite big_mkord (bigD1 floor_f)//= ifT; last first. + rewrite lee_fin lte_fin; apply/andP; split. + by rewrite natr_absz (@ger0_norm _ (floor `|f t|)) ?floor_ge0 ?floor_le. + rewrite -addn1 natrD natr_absz. + by rewrite (@ger0_norm _ (floor `|f t|)) ?floor_ge0 ?lt_succ_floor. +rewrite big1 ?adde0//= => j jk. +rewrite ifF// lte_fin lee_fin. +move: jk; rewrite neq_ltn/= => /orP[|] jr. +- suff : (j.+1%:R <= `|f t|)%R by rewrite leNgt => /negbTE ->; rewrite andbF. + rewrite (_ : j.+1%:R = j.+1%:~R)// floor_ge_int. + move: jr; rewrite -lez_nat => /le_trans; apply. + by rewrite -[leRHS](@ger0_norm _ (floor `|f t|)) ?floor_ge0. +- suff : (`|f t| < j%:R)%R by rewrite ltNge => /negbTE ->. + move: jr; rewrite -ltz_nat -(@ltr_int R) (@gez0_abs (floor `|f t|)) ?floor_ge0//. + by rewrite ltr_int -floor_lt_int. +Qed. + +HB.instance Definition _ := + @Kernel_isSFinite.Build _ _ _ _ _ (kscore mf) sfinite_kscore. + +End kscore. + +(* decomposition of ite into s-finite kernels *) +Module ITE. +Section ite. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). + +Section kiteT. +Variable k : R.-ker X ~> Y. + +Definition kiteT : X * bool -> {measure set Y -> \bar R} := + fun xb => if xb.2 then k xb.1 else [the measure _ _ of mzero]. + +Let measurable_fun_kiteT U : measurable U -> measurable_fun setT (kiteT ^~ U). +Proof. +move=> /= mcU; rewrite /kiteT. +rewrite (_ : (fun _ => _) = + (fun x => if x.2 then k x.1 U else mzero U)); last first. + by apply/funext => -[t b]/=; case: ifPn. +apply: (@measurable_fun_if_pair _ _ _ _ (k ^~ U) (fun=> mzero U)) => //. +exact/measurable_kernel. +Qed. + +#[export] +HB.instance Definition _ := isKernel.Build _ _ _ _ _ + kiteT measurable_fun_kiteT. +End kiteT. + +Section sfkiteT. +Variable k : R.-sfker X ~> Y. + +Let sfinite_kiteT : exists2 k_ : (R.-ker _ ~> _)^nat, + forall n, measure_fam_uub (k_ n) & + forall x U, measurable U -> kiteT k x U = mseries (k_ ^~ x) 0 U. +Proof. +have [k_ hk /=] := sfinite_kernel k. +exists (fun n => [the _.-ker _ ~> _ of kiteT (k_ n)]) => /=. + move=> n; have /measure_fam_uubP[r k_r] := measure_uub (k_ n). + by exists r%:num => /= -[x []]; rewrite /kiteT//= /mzero//. +move=> [x b] U mU; rewrite /kiteT; case: ifPn => hb; first by rewrite hk. +by rewrite /mseries eseries0. +Qed. + +#[export] +HB.instance Definition _ t := @Kernel_isSFinite_subdef.Build _ _ _ _ _ + (kiteT k) sfinite_kiteT. +End sfkiteT. + +Section fkiteT. +Variable k : R.-fker X ~> Y. + +Let kiteT_uub : measure_fam_uub (kiteT k). +Proof. +have /measure_fam_uubP[M hM] := measure_uub k. +exists M%:num => /= -[]; rewrite /kiteT => t [|]/=; first exact: hM. +by rewrite /= /mzero. +Qed. + +#[export] +HB.instance Definition _ t := Kernel_isFinite.Build _ _ _ _ _ + (kiteT k) kiteT_uub. +End fkiteT. + +Section kiteF. +Variable k : R.-ker X ~> Y. + +Definition kiteF : X * bool -> {measure set Y -> \bar R} := + fun xb => if ~~ xb.2 then k xb.1 else [the measure _ _ of mzero]. + +Let measurable_fun_kiteF U : measurable U -> measurable_fun setT (kiteF ^~ U). +Proof. +move=> /= mcU; rewrite /kiteF. +rewrite (_ : (fun x => _) = + (fun x => if x.2 then mzero U else k x.1 U)); last first. + by apply/funext => -[t b]/=; rewrite if_neg//; case: ifPn. +apply: (@measurable_fun_if_pair _ _ _ _ (fun=> mzero U) (k ^~ U)) => //. +exact/measurable_kernel. +Qed. + +#[export] +HB.instance Definition _ := isKernel.Build _ _ _ _ _ + kiteF measurable_fun_kiteF. + +End kiteF. + +Section sfkiteF. +Variable k : R.-sfker X ~> Y. + +Let sfinite_kiteF : exists2 k_ : (R.-ker _ ~> _)^nat, + forall n, measure_fam_uub (k_ n) & + forall x U, measurable U -> kiteF k x U = mseries (k_ ^~ x) 0 U. +Proof. +have [k_ hk /=] := sfinite_kernel k. +exists (fun n => [the _.-ker _ ~> _ of kiteF (k_ n)]) => /=. + move=> n; have /measure_fam_uubP[r k_r] := measure_uub (k_ n). + by exists r%:num => /= -[x []]; rewrite /kiteF//= /mzero//. +move=> [x b] U mU; rewrite /kiteF; case: ifPn => hb; first by rewrite hk. +by rewrite /mseries eseries0. +Qed. + +#[export] +HB.instance Definition _ := @Kernel_isSFinite_subdef.Build _ _ _ _ _ + (kiteF k) sfinite_kiteF. + +End sfkiteF. + +Section fkiteF. +Variable k : R.-fker X ~> Y. + +Let kiteF_uub : measure_fam_uub (kiteF k). +Proof. +have /measure_fam_uubP[M hM] := measure_uub k. +by exists M%:num => /= -[]; rewrite /kiteF/= => t; case => //=; rewrite /mzero. +Qed. + +#[export] +HB.instance Definition _ := Kernel_isFinite.Build _ _ _ _ _ + (kiteF k) kiteF_uub. + +End fkiteF. +End ite. +End ITE. + +Section ite. +Context d d' (T : measurableType d) (T' : measurableType d') (R : realType). +Variables (f : T -> bool) (u1 u2 : R.-sfker T ~> T'). + +Definition mite (mf : measurable_fun setT f) : T -> set T' -> \bar R := + fun t => if f t then u1 t else u2 t. + +Variables mf : measurable_fun setT f. + +Let mite0 t : mite mf t set0 = 0. +Proof. by rewrite /mite; case: ifPn. Qed. + +Let mite_ge0 t U : 0 <= mite mf t U. +Proof. by rewrite /mite; case: ifPn. Qed. + +Let mite_sigma_additive t : semi_sigma_additive (mite mf t). +Proof. +by rewrite /mite; case: ifPn => ft; exact: measure_semi_sigma_additive. +Qed. + +HB.instance Definition _ t := isMeasure.Build _ _ _ (mite mf t) + (mite0 t) (mite_ge0 t) (@mite_sigma_additive t). + +Import ITE. + +(* +Definition kite : R.-sfker T ~> T' := + kdirac mf \; kadd (kiteT u1) (kiteF u2). +*) +Definition kite := + [the R.-sfker _ ~> _ of kdirac mf] \; + [the R.-sfker _ ~> _ of kadd + [the R.-sfker _ ~> T' of kiteT u1] + [the R.-sfker _ ~> T' of kiteF u2] ]. + +End ite. + +Section insn2. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). + +Definition ret (f : X -> Y) (mf : measurable_fun setT f) + : R.-pker X ~> Y := [the R.-pker _ ~> _ of kdirac mf]. + +Definition sample (P : pprobability Y R) : R.-pker X ~> Y := + [the R.-pker _ ~> _ of kprobability (measurable_cst P)]. + +Definition normalize (k : R.-sfker X ~> Y) P : X -> probability Y R := + fun x => [the probability _ _ of mnormalize (k x) P]. + +Definition ite (f : X -> bool) (mf : measurable_fun setT f) + (k1 k2 : R.-sfker X ~> Y) : R.-sfker X ~> Y := + locked [the R.-sfker X ~> Y of kite k1 k2 mf]. + +End insn2. +Arguments ret {d d' X Y R f} mf. +Arguments sample {d d' X Y R}. + +Section insn2_lemmas. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). + +Lemma retE (f : X -> Y) (mf : measurable_fun setT f) x : + ret mf x = \d_(f x) :> (_ -> \bar R). +Proof. by []. Qed. + +Lemma sampleE (P : probability Y R) (x : X) : sample P x = P. +Proof. by []. Qed. + +Lemma normalizeE (f : R.-sfker X ~> Y) P x U : + normalize f P x U = + if (f x [set: Y] == 0) || (f x [set: Y] == +oo) then P U + else f x U * ((fine (f x [set: Y]))^-1)%:E. +Proof. by rewrite /normalize /= /mnormalize; case: ifPn. Qed. + +Lemma iteE (f : X -> bool) (mf : measurable_fun setT f) + (k1 k2 : R.-sfker X ~> Y) x : + ite mf k1 k2 x = if f x then k1 x else k2 x. +Proof. +apply/eq_measure/funext => U. +rewrite /ite; unlock => /=. +rewrite /kcomp/= integral_dirac//=. +rewrite indicT mul1e. +rewrite -/(measure_add (ITE.kiteT k1 (x, f x)) (ITE.kiteF k2 (x, f x))). +rewrite measure_addE. +rewrite /ITE.kiteT /ITE.kiteF/=. +by case: ifPn => fx /=; rewrite /mzero ?(adde0,add0e). +Qed. + +End insn2_lemmas. + +Section insn3. +Context d d' d3 (X : measurableType d) (Y : measurableType d') + (Z : measurableType d3) (R : realType). + +Definition letin (l : R.-sfker X ~> Y) (k : R.-sfker [the measurableType _ of (X * Y)%type] ~> Z) + : R.-sfker X ~> Z := + [the R.-sfker X ~> Z of l \; k]. + +End insn3. + +Section insn3_lemmas. +Context d d' d3 (X : measurableType d) (Y : measurableType d') + (Z : measurableType d3) (R : realType). + +Lemma letinE (l : R.-sfker X ~> Y) (k : R.-sfker [the measurableType _ of (X * Y)%type] ~> Z) x U : + letin l k x U = \int[l x]_y k (x, y) U. +Proof. by []. Qed. + +End insn3_lemmas. + +(* rewriting laws *) +Section letin_return. +Context d d' d3 (X : measurableType d) (Y : measurableType d') + (Z : measurableType d3) (R : realType). + +Lemma letin_kret (k : R.-sfker X ~> Y) + (f : X * Y -> Z) (mf : measurable_fun setT f) x U : + measurable U -> + letin k (ret mf) x U = k x (curry f x @^-1` U). +Proof. +move=> mU; rewrite letinE. +under eq_integral do rewrite retE. +rewrite integral_indic ?setIT// -[X in measurable X]setTI. +exact: (measurableT_comp mf). +Qed. + +Lemma letin_retk + (f : X -> Y) (mf : measurable_fun setT f) + (k : R.-sfker [the measurableType _ of (X * Y)%type] ~> Z) + x U : measurable U -> + letin (ret mf) k x U = k (x, f x) U. +Proof. +move=> mU; rewrite letinE retE integral_dirac ?indicT ?mul1e//. +exact: (measurableT_comp (measurable_kernel k _ mU)). +Qed. + +End letin_return. + +Section insn1. +Context d (X : measurableType d) (R : realType). + +Definition score (f : X -> R) (mf : measurable_fun setT f) + : R.-sfker X ~> _ := + [the R.-sfker X ~> _ of kscore mf]. + +End insn1. + +Section hard_constraint. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). + +Definition fail := + letin (score (@measurable_cst _ _ X _ setT (0%R : R))) + (ret (@measurable_cst _ _ _ Y setT point)). + +Lemma failE x U : fail x U = 0. +Proof. by rewrite /fail letinE ge0_integral_mscale//= normr0 mul0e. Qed. + +End hard_constraint. +Arguments fail {d d' X Y R}. + +Module Notations. + +Notation var1of2 := (@measurable_fst _ _ _ _). +Notation var2of2 := (@measurable_snd _ _ _ _). +Notation var1of3 := (measurableT_comp (@measurable_fst _ _ _ _) + (@measurable_fst _ _ _ _)). +Notation var2of3 := (measurableT_comp (@measurable_snd _ _ _ _) + (@measurable_fst _ _ _ _)). +Notation var3of3 := (@measurable_snd _ _ _ _). + +Notation mR := Real_sort__canonical__measure_Measurable. +Notation munit := Datatypes_unit__canonical__measure_Measurable. +Notation mbool := Datatypes_bool__canonical__measure_Measurable. + +End Notations. + +Section cst_fun. +Context d (T : measurableType d) (R : realType). + +Definition kr (r : R) := @measurable_cst _ _ T _ setT r. +Definition k3 : measurable_fun _ _ := kr 3%:R. +Definition k10 : measurable_fun _ _ := kr 10%:R. +Definition ktt := @measurable_cst _ _ T _ setT tt. + +End cst_fun. +Arguments kr {d T R}. +Arguments k3 {d T R}. +Arguments k10 {d T R}. +Arguments ktt {d T}. + +Section insn1_lemmas. +Import Notations. +Context d (T : measurableType d) (R : realType). + +Let kcomp_scoreE d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) + (g : R.-sfker [the measurableType _ of (T1 * unit)%type] ~> T2) + f (mf : measurable_fun setT f) r U : + (score mf \; g) r U = `|f r|%:E * g (r, tt) U. +Proof. +rewrite /= /kcomp /kscore /= ge0_integral_mscale//=. +by rewrite integral_dirac// indicT mul1e. +Qed. + +Lemma scoreE d' (T' : measurableType d') (x : T * T') (U : set T') (f : R -> R) + (r : R) (r0 : (0 <= r)%R) + (f0 : (forall r, 0 <= r -> 0 <= f r)%R) (mf : measurable_fun setT f) : + score (measurableT_comp mf var2of2) + (x, r) (curry (snd \o fst) x @^-1` U) = + (f r)%:E * \d_x.2 U. +Proof. by rewrite /score/= /mscale/= ger0_norm// f0. Qed. + +Lemma score_score (f : R -> R) (g : R * unit -> R) + (mf : measurable_fun setT f) + (mg : measurable_fun setT g) : + letin (score mf) (score mg) = + score (measurable_funM mf (measurableT_comp mg (measurable_pair2 tt))). +Proof. +apply/eq_sfkernel => x U. +rewrite {1}/letin; unlock. +by rewrite kcomp_scoreE/= /mscale/= diracE normrM muleA EFinM. +Qed. + +Import Notations. + +(* hard constraints to express score below 1 *) +Lemma score_fail (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : + score (kr r%:num) = + letin (sample [the probability _ _ of bernoulli r1] : R.-pker T ~> _) + (ite var2of2 (ret ktt) fail). +Proof. +apply/eq_sfkernel => x U. +rewrite letinE/= /sample; unlock. +rewrite integral_measure_add//= ge0_integral_mscale//= ge0_integral_mscale//=. +rewrite integral_dirac//= integral_dirac//= !indicT/= !mul1e. +by rewrite /mscale/= iteE//= iteE//= failE mule0 adde0 ger0_norm. +Qed. + +End insn1_lemmas. + +Section letin_ite. +Context d d2 d3 (T : measurableType d) (T2 : measurableType d2) + (Z : measurableType d3) (R : realType). +Variables (k1 k2 : R.-sfker T ~> Z) (u : R.-sfker [the measurableType _ of (T * Z)%type] ~> T2) + (f : T -> bool) (mf : measurable_fun setT f) + (t : T) (U : set T2). + +Lemma letin_iteT : f t -> letin (ite mf k1 k2) u t U = letin k1 u t U. +Proof. +move=> ftT. +rewrite !letinE/=. +apply: eq_measure_integral => V mV _. +by rewrite iteE ftT. +Qed. + +Lemma letin_iteF : ~~ f t -> letin (ite mf k1 k2) u t U = letin k2 u t U. +Proof. +move=> ftF. +rewrite !letinE/=. +apply: eq_measure_integral => V mV _. +by rewrite iteE (negbTE ftF). +Qed. + +End letin_ite. + +Section letinA. +Context d d' d1 d2 d3 (X : measurableType d) (Y : measurableType d') + (T1 : measurableType d1) (T2 : measurableType d2) (T3 : measurableType d3) + (R : realType). +Import Notations. +Variables (t : R.-sfker X ~> T1) + (u : R.-sfker [the measurableType _ of (X * T1)%type] ~> T2) + (v : R.-sfker [the measurableType _ of (X * T2)%type] ~> Y) + (v' : R.-sfker [the measurableType _ of (X * T1 * T2)%type] ~> Y) + (vv' : forall y, v =1 fun xz => v' (xz.1, y, xz.2)). + +Lemma letinA x A : measurable A -> + letin t (letin u v') x A + = + (letin (letin t u) v) x A. +Proof. +move=> mA. +rewrite !letinE. +under eq_integral do rewrite letinE. +rewrite integral_kcomp; [|by []|]. +- apply: eq_integral => y _. + apply: eq_integral => z _. + by rewrite (vv' y). +exact: (measurableT_comp (measurable_kernel v _ mA)). +Qed. + +End letinA. + +Section letinC. +Context d d1 d' (X : measurableType d) (Y : measurableType d1) + (Z : measurableType d') (R : realType). + +Import Notations. + +Variables (t : R.-sfker Z ~> X) + (t' : R.-sfker [the measurableType _ of (Z * Y)%type] ~> X) + (tt' : forall y, t =1 fun z => t' (z, y)) + (u : R.-sfker Z ~> Y) + (u' : R.-sfker [the measurableType _ of (Z * X)%type] ~> Y) + (uu' : forall x, u =1 fun z => u' (z, x)). + +Definition T z : set X -> \bar R := t z. +Let T0 z : (T z) set0 = 0. Proof. by []. Qed. +Let T_ge0 z x : 0 <= (T z) x. Proof. by []. Qed. +Let T_semi_sigma_additive z : semi_sigma_additive (T z). +Proof. exact: measure_semi_sigma_additive. Qed. +HB.instance Definition _ z := @isMeasure.Build _ R X (T z) (T0 z) (T_ge0 z) + (@T_semi_sigma_additive z). + +Let sfinT z : sfinite_measure (T z). Proof. exact: sfinite_kernel_measure. Qed. +HB.instance Definition _ z := @Measure_isSFinite_subdef.Build _ X R + (T z) (sfinT z). + +Definition U z : set Y -> \bar R := u z. +Let U0 z : (U z) set0 = 0. Proof. by []. Qed. +Let U_ge0 z x : 0 <= (U z) x. Proof. by []. Qed. +Let U_semi_sigma_additive z : semi_sigma_additive (U z). +Proof. exact: measure_semi_sigma_additive. Qed. +HB.instance Definition _ z := @isMeasure.Build _ R Y (U z) (U0 z) (U_ge0 z) + (@U_semi_sigma_additive z). + +Let sfinU z : sfinite_measure (U z). Proof. exact: sfinite_kernel_measure. Qed. +HB.instance Definition _ z := @Measure_isSFinite_subdef.Build _ Y R + (U z) (sfinU z). + +Lemma letinC z A : measurable A -> + letin t + (letin u' + (ret (measurable_fun_prod var2of3 var3of3))) z A = + letin u + (letin t' + (ret (measurable_fun_prod var3of3 var2of3))) z A. +Proof. +move=> mA. +rewrite !letinE. +under eq_integral. + move=> x _. + rewrite letinE -uu'. + under eq_integral do rewrite retE /=. + over. +rewrite (sfinite_Fubini + [the {sfinite_measure set X -> \bar R} of T z] + [the {sfinite_measure set Y -> \bar R} of U z] + (fun x => \d_(x.1, x.2) A ))//; last first. + apply/EFin_measurable_fun => /=; rewrite (_ : (fun x => _) = mindic R mA)//. + by apply/funext => -[]. +rewrite /=. +apply: eq_integral => y _. +by rewrite letinE/= -tt'; apply: eq_integral => // x _; rewrite retE. +Qed. + +End letinC. + +(* sample programs *) + +Section constants. +Variable R : realType. +Local Open Scope ring_scope. + +Lemma onem1S n : `1- (1 / n.+1%:R) = (n%:R / n.+1%:R)%:nng%:num :> R. +Proof. +by rewrite /onem/= -{1}(@divrr _ n.+1%:R) ?unitfE// -mulrBl -natr1 addrK. +Qed. + +Lemma p1S n : (1 / n.+1%:R)%:nng%:num <= 1 :> R. +Proof. by rewrite ler_pdivr_mulr//= mul1r ler1n. Qed. + +Lemma p12 : (1 / 2%:R)%:nng%:num <= 1 :> R. Proof. by rewrite p1S. Qed. + +Lemma p14 : (1 / 4%:R)%:nng%:num <= 1 :> R. Proof. by rewrite p1S. Qed. + +Lemma onem27 : `1- (2 / 7%:R) = (5%:R / 7%:R)%:nng%:num :> R. +Proof. by apply/eqP; rewrite subr_eq/= -mulrDl -natrD divrr// unitfE. Qed. + +Lemma p27 : (2 / 7%:R)%:nng%:num <= 1 :> R. +Proof. by rewrite /= lter_pdivr_mulr// mul1r ler_nat. Qed. + +End constants. +Arguments p12 {R}. +Arguments p14 {R}. +Arguments p27 {R}. + +Section poisson. +Variable R : realType. +Local Open Scope ring_scope. + +(* density function for Poisson *) +Definition poisson k r : R := r ^+ k / k`!%:R^-1 * expR (- r). + +Lemma poisson_ge0 k r : 0 <= r -> 0 <= poisson k r. +Proof. +move=> r0; rewrite /poisson mulr_ge0 ?expR_ge0//. +by rewrite mulr_ge0// exprn_ge0. +Qed. + +Lemma poisson_gt0 k r : 0 < r -> 0 < poisson k.+1 r. +Proof. +move=> r0; rewrite /poisson mulr_gt0 ?expR_gt0//. +by rewrite divr_gt0// ?exprn_gt0// invr_gt0 ltr0n fact_gt0. +Qed. + +Lemma mpoisson k : measurable_fun setT (poisson k). +Proof. +by apply: measurable_funM => /=; [exact: measurable_funM|exact: measurableT_comp]. +Qed. + +Definition poisson3 := poisson 4 3%:R. (* 0.168 *) +Definition poisson10 := poisson 4 10%:R. (* 0.019 *) + +End poisson. + +Section exponential. +Variable R : realType. +Local Open Scope ring_scope. + +(* density function for exponential *) +Definition exp_density x r : R := r * expR (- r * x). + +Lemma exp_density_gt0 x r : 0 < r -> 0 < exp_density x r. +Proof. by move=> r0; rewrite /exp_density mulr_gt0// expR_gt0. Qed. + +Lemma exp_density_ge0 x r : 0 <= r -> 0 <= exp_density x r. +Proof. by move=> r0; rewrite /exp_density mulr_ge0// expR_ge0. Qed. + +Lemma mexp_density x : measurable_fun setT (exp_density x). +Proof. +apply: measurable_funM => //=; apply: measurableT_comp => //. +exact: measurable_funM. +Qed. + +End exponential. + +Lemma letin_sample_bernoulli d d' (T : measurableType d) + (T' : measurableType d') (R : realType)(r : {nonneg R}) (r1 : (r%:num <= 1)%R) + (u : R.-sfker [the measurableType _ of (T * bool)%type] ~> T') x y : + letin (sample [the probability _ _ of bernoulli r1]) u x y = + r%:num%:E * u (x, true) y + (`1- (r%:num))%:E * u (x, false) y. +Proof. +rewrite letinE/=. +rewrite ge0_integral_measure_sum// 2!big_ord_recl/= big_ord0 adde0/=. +by rewrite !ge0_integral_mscale//= !integral_dirac//= indicT 2!mul1e. +Qed. + +Section sample_and_return. +Import Notations. +Context d (T : measurableType d) (R : realType). + +Definition sample_and_return : R.-sfker T ~> _ := + letin + (sample [the probability _ _ of bernoulli p27]) (* T -> B *) + (ret var2of2) (* T * B -> B *). + +Lemma sample_and_returnE t U : sample_and_return t U = + (2 / 7%:R)%:E * \d_true U + (5%:R / 7%:R)%:E * \d_false U. +Proof. +by rewrite /sample_and_return letin_sample_bernoulli !retE onem27. +Qed. + +End sample_and_return. + +(* trivial example *) +Section sample_and_branch. +Import Notations. +Context d (T : measurableType d) (R : realType). + +(* let x = sample (bernoulli (2/7)) in + let r = case x of {(1, _) => return (k3()), (2, _) => return (k10())} in + return r *) + +Definition sample_and_branch : R.-sfker T ~> mR R := + letin + (sample [the probability _ _ of bernoulli p27]) (* T -> B *) + (ite var2of2 (ret k3) (ret k10)). + +Lemma sample_and_branchE t U : sample_and_branch t U = + (2 / 7%:R)%:E * \d_(3%:R : R) U + + (5%:R / 7%:R)%:E * \d_(10%:R : R) U. +Proof. +by rewrite /sample_and_branch letin_sample_bernoulli/= !iteE !retE onem27. +Qed. + +End sample_and_branch. + +Section bernoulli_and. +Context d (T : measurableType d) (R : realType). +Import Notations. + +Definition mand (x y : T * mbool * mbool -> mbool) + (t : T * mbool * mbool) : mbool := x t && y t. + +Lemma measurable_fun_mand (x y : T * mbool * mbool -> mbool) : + measurable_fun setT x -> measurable_fun setT y -> + measurable_fun setT (mand x y). +Proof. +move=> /= mx my; apply: (@measurable_fun_bool _ _ _ _ true). +rewrite [X in measurable X](_ : _ = + (x @^-1` [set true]) `&` (y @^-1` [set true])); last first. + by rewrite /mand; apply/seteqP; split => z/= /andP. +apply: measurableI. +- by rewrite -[X in measurable X]setTI; exact: mx. +- by rewrite -[X in measurable X]setTI; exact: my. +Qed. + +Definition bernoulli_and : R.-sfker T ~> mbool := + (letin (sample [the probability _ _ of bernoulli p12]) + (letin (sample [the probability _ _ of bernoulli p12]) + (ret (measurable_fun_mand var2of3 var3of3)))). + +Lemma bernoulli_andE t U : + bernoulli_and t U = + sample [the probability _ _ of bernoulli p14] t U. +Proof. +rewrite /bernoulli_and 3!letin_sample_bernoulli/= /mand/= muleDr//= -muleDl//. +rewrite !muleA -addeA -muleDl// -!EFinM !onem1S/= -splitr mulr1. +have -> : (1 / 2 * (1 / 2) = 1 / 4%:R :> R)%R by rewrite mulf_div mulr1// -natrM. +rewrite /bernoulli/= measure_addE/= /mscale/= -!EFinM; congr( _ + (_ * _)%:E). +have -> : (1 / 2 = 2 / 4%:R :> R)%R. + by apply/eqP; rewrite eqr_div// ?pnatr_eq0// mul1r -natrM. +by rewrite onem1S// -mulrDl. +Qed. + +End bernoulli_and. + +Section staton_bus. +Import Notations. +Context d (T : measurableType d) (R : realType) (h : R -> R). +Hypothesis mh : measurable_fun setT h. +Definition kstaton_bus : R.-sfker T ~> mbool := + letin (sample [the probability _ _ of bernoulli p27]) + (letin + (letin (ite var2of2 (ret k3) (ret k10)) + (score (measurableT_comp mh var3of3))) + (ret var2of3)). + +Definition staton_bus := normalize kstaton_bus. + +End staton_bus. + +(* let x = sample (bernoulli (2/7)) in + let r = case x of {(1, _) => return (k3()), (2, _) => return (k10())} in + let _ = score (1/4! r^4 e^-r) in + return x *) +Section staton_bus_poisson. +Import Notations. +Context d (T : measurableType d) (R : realType). +Let poisson4 := @poisson R 4%N. +Let mpoisson4 := @mpoisson R 4%N. + +Definition kstaton_bus_poisson : R.-sfker (mR R) ~> mbool := + kstaton_bus _ mpoisson4. + +Let kstaton_bus_poissonE t U : kstaton_bus_poisson t U = + (2 / 7%:R)%:E * (poisson4 3%:R)%:E * \d_true U + + (5%:R / 7%:R)%:E * (poisson4 10%:R)%:E * \d_false U. +Proof. +rewrite /kstaton_bus. +rewrite letin_sample_bernoulli. +rewrite -!muleA; congr (_ * _ + _ * _). +- rewrite letin_kret//. + rewrite letin_iteT//. + rewrite letin_retk//. + by rewrite scoreE//= => r r0; exact: poisson_ge0. +- by rewrite onem27. + rewrite letin_kret//. + rewrite letin_iteF//. + rewrite letin_retk//. + by rewrite scoreE//= => r r0; exact: poisson_ge0. +Qed. + +(* true -> 2/7 * 0.168 = 2/7 * 3^4 e^-3 / 4! *) +(* false -> 5/7 * 0.019 = 5/7 * 10^4 e^-10 / 4! *) + +Lemma staton_busE P (t : R) U : + let N := ((2 / 7%:R) * poisson4 3%:R + + (5%:R / 7%:R) * poisson4 10%:R)%R in + staton_bus mpoisson4 P t U = + ((2 / 7%:R)%:E * (poisson4 3%:R)%:E * \d_true U + + (5%:R / 7%:R)%:E * (poisson4 10%:R)%:E * \d_false U) * N^-1%:E. +Proof. +rewrite /staton_bus normalizeE /= !kstaton_bus_poissonE !diracT !mule1 ifF //. +apply/negbTE; rewrite gt_eqF// lte_fin. +by rewrite addr_gt0// mulr_gt0//= ?divr_gt0// ?ltr0n// poisson_gt0// ltr0n. +Qed. + +End staton_bus_poisson. + +(* let x = sample (bernoulli (2/7)) in + let r = case x of {(1, _) => return (k3()), (2, _) => return (k10())} in + let _ = score (r e^-(15/60 r)) in + return x *) +Section staton_bus_exponential. +Import Notations. +Context d (T : measurableType d) (R : realType). +Let exp1560 := @exp_density R (ratr (15%:Q / 60%:Q)). +Let mexp1560 := @mexp_density R (ratr (15%:Q / 60%:Q)). + +(* 15/60 = 0.25 *) + +Definition kstaton_bus_exponential : R.-sfker (mR R) ~> mbool := + kstaton_bus _ mexp1560. + +Let kstaton_bus_exponentialE t U : kstaton_bus_exponential t U = + (2 / 7%:R)%:E * (exp1560 3%:R)%:E * \d_true U + + (5%:R / 7%:R)%:E * (exp1560 10%:R)%:E * \d_false U. +Proof. +rewrite /kstaton_bus. +rewrite letin_sample_bernoulli. +rewrite -!muleA; congr (_ * _ + _ * _). +- rewrite letin_kret//. + rewrite letin_iteT//. + rewrite letin_retk//. + rewrite scoreE//= => r r0; exact: exp_density_ge0. +- by rewrite onem27. + rewrite letin_kret//. + rewrite letin_iteF//. + rewrite letin_retk//. + by rewrite scoreE//= => r r0; exact: exp_density_ge0. +Qed. + +(* true -> 5/7 * 0.019 = 5/7 * 10^4 e^-10 / 4! *) +(* false -> 2/7 * 0.168 = 2/7 * 3^4 e^-3 / 4! *) + +Lemma staton_bus_exponentialE P (t : R) U : + let N := ((2 / 7%:R) * exp1560 3%:R + + (5%:R / 7%:R) * exp1560 10%:R)%R in + staton_bus mexp1560 P t U = + ((2 / 7%:R)%:E * (exp1560 3%:R)%:E * \d_true U + + (5%:R / 7%:R)%:E * (exp1560 10%:R)%:E * \d_false U) * N^-1%:E. +Proof. +rewrite /staton_bus. +rewrite normalizeE /= !kstaton_bus_exponentialE !diracT !mule1 ifF //. +apply/negbTE; rewrite gt_eqF// lte_fin. +by rewrite addr_gt0// mulr_gt0//= ?divr_gt0// ?ltr0n// exp_density_gt0 ?ltr0n. +Qed. + +End staton_bus_exponential. diff --git a/theories/prob_lang_wip.v b/theories/prob_lang_wip.v new file mode 100644 index 000000000..7b12eed1b --- /dev/null +++ b/theories/prob_lang_wip.v @@ -0,0 +1,148 @@ +From HB Require Import structures. +From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval finmap. +From mathcomp Require Import rat. +From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. +From mathcomp.classical Require Import functions cardinality fsbigop. +Require Import signed reals ereal topology normedtype sequences esum measure. +Require Import lebesgue_measure numfun lebesgue_integral exp kernel trigo. +Require Import prob_lang. + +(******************************************************************************) +(* Semantics of a probabilistic programming language using s-finite kernels *) +(* (wip) *) +(******************************************************************************) + +Set Implicit Arguments. +Unset Strict Implicit. +Unset Printing Implicit Defensive. +Import Order.TTheory GRing.Theory Num.Def Num.ExtraDef Num.Theory. +Import numFieldTopology.Exports. + +Local Open Scope classical_set_scope. +Local Open Scope ring_scope. +Local Open Scope ereal_scope. + +Section gauss. +Variable R : realType. +Local Open Scope ring_scope. + +(* density function for gauss *) +Definition gauss_density m s x : R := + (s * sqrtr (pi *+ 2))^-1 * expR (- ((x - m) / s) ^+ 2 / 2%:R). + +Lemma gauss_density_ge0 m s x : 0 <= s -> 0 <= gauss_density m s x. +Proof. by move=> s0; rewrite mulr_ge0 ?expR_ge0// invr_ge0 mulr_ge0. Qed. + +Lemma gauss_density_gt0 m s x : 0 < s -> 0 < gauss_density m s x. +Proof. +move=> s0; rewrite mulr_gt0 ?expR_gt0// invr_gt0 mulr_gt0//. +by rewrite sqrtr_gt0 pmulrn_rgt0// pi_gt0. +Qed. + +Definition gauss01_density : R -> R := gauss_density 0 1. + +Lemma gauss01_densityE x : + gauss01_density x = (sqrtr (pi *+ 2))^-1 * expR (- (x ^+ 2) / 2%:R). +Proof. by rewrite /gauss01_density /gauss_density mul1r subr0 divr1. Qed. + +Definition mgauss01 (V : set R) := + (\int[lebesgue_measure]_(x in V) (gauss01_density x)%:E)%E. + +Lemma measurable_fun_gauss_density m s : + measurable_fun setT (gauss_density m s). +Proof. +apply: measurable_funM => //=. +apply: measurableT_comp => //=. +apply: measurable_funM => //=. +apply: measurableT_comp => //=. +apply: measurableT_comp (measurable_exprn _) _ => /=. +apply: measurable_funM => //=. +exact: measurable_funD. +Qed. + +Let mgauss010 : mgauss01 set0 = 0%E. +Proof. by rewrite /mgauss01 integral_set0. Qed. + +Let mgauss01_ge0 A : (0 <= mgauss01 A)%E. +Proof. +by rewrite /mgauss01 integral_ge0//= => x _; rewrite lee_fin gauss_density_ge0. +Qed. + +Axiom integral_gauss01_density : + (\int[lebesgue_measure]_x (gauss01_density x)%:E = 1%E)%E. + +Let mgauss01_sigma_additive : semi_sigma_additive mgauss01. +Proof. +move=> /= F mF tF mUF. +rewrite /mgauss01/= integral_bigcup//=; last first. + apply/integrableP; split. + apply/EFin_measurable_fun. + exact: measurable_funS (measurable_fun_gauss_density 0 1). + rewrite (_ : (fun x => _) = (EFin \o gauss01_density)); last first. + by apply/funext => x; rewrite gee0_abs// lee_fin gauss_density_ge0. + apply: le_lt_trans. + apply: (@subset_integral _ _ _ _ _ setT) => //=. + apply/EFin_measurable_fun. + exact: measurable_fun_gauss_density. + by move=> ? _; rewrite lee_fin gauss_density_ge0. + by rewrite integral_gauss01_density// ltey. +apply: is_cvg_ereal_nneg_natsum_cond => n _ _. +by apply: integral_ge0 => /= x ?; rewrite lee_fin gauss_density_ge0. +Qed. + +HB.instance Definition _ := isMeasure.Build _ _ _ + mgauss01 mgauss010 mgauss01_ge0 mgauss01_sigma_additive. + +Let mgauss01_setT : mgauss01 [set: _] = 1%E. +Proof. by rewrite /mgauss01 integral_gauss01_density. Qed. + +HB.instance Definition _ := @Measure_isProbability.Build _ _ R mgauss01 mgauss01_setT. + +Definition gauss01 := [the probability _ _ of mgauss01]. + +End gauss. + +Section gauss_lebesgue. +Import Notations. +Context d (T : measurableType d) (R : realType). + +Let f1 (x : R) := (gauss01_density x) ^-1. + +Let mf1 : measurable_fun setT f1. +Proof. +apply: (measurable_comp (F := [set r : R | r != 0%R])) => //. +- exact: open_measurable. +- by move=> /= r [t _ <-]; rewrite gt_eqF// gauss_density_gt0. +- apply: open_continuous_measurable_fun => //. + by apply/in_setP => x /= x0; exact: inv_continuous. +- exact: measurable_fun_gauss_density. +Qed. + +Variable mu : {measure set mR R -> \bar R}. + +Definition staton_lebesgue : R.-sfker T ~> _ := + letin (sample (@gauss01 R)) + (letin + (score (measurableT_comp mf1 var2of2)) + (ret var2of3)). + +Lemma staton_lebesgueE x U : measurable U -> + staton_lebesgue x U = lebesgue_measure U. +Proof. +move=> mU; rewrite [in LHS]/staton_lebesgue/=. +rewrite [in LHS]letinE /=. +transitivity (\int[@mgauss01 R]_(y in U) (f1 y)%:E). + rewrite -[in RHS](setTI U) integral_setI_indic//=. + apply: eq_integral => /= r _. + rewrite letinE/= ge0_integral_mscale//= ger0_norm//; last first. + by rewrite invr_ge0// gauss_density_ge0. + by rewrite integral_dirac// indicT mul1e diracE indicE. +transitivity (\int[lebesgue_measure]_(x in U) (gauss01_density x * f1 x)%:E). + admit. +transitivity (\int[lebesgue_measure]_(x in U) (\1_U x)%:E). + apply: eq_integral => /= y yU. + by rewrite /f1 divrr ?indicE ?yU// unitfE gt_eqF// gauss_density_gt0. +by rewrite integral_indic//= setIid. +Abort. + +End gauss_lebesgue. From 21c94212e55d588f694a155806a2b7d4c6294e4d Mon Sep 17 00:00:00 2001 From: AyumuSaito Date: Tue, 14 Feb 2023 22:02:38 +0900 Subject: [PATCH 07/36] syntax and denotational semantics Co-authored-by: Reynald Affeldt --- _CoqProject | 4 + coq-mathcomp-analysis.opam | 1 + theories/Make | 4 + theories/lang_syntax.v | 1119 +++++++++++++++++++++++++++++++ theories/lang_syntax_examples.v | 784 ++++++++++++++++++++++ theories/lang_syntax_toy.v | 550 +++++++++++++++ theories/lang_syntax_util.v | 80 +++ theories/measure.v | 4 +- theories/prob_lang.v | 986 +++++++++++++++++++++++++-- theories/prob_lang_wip.v | 125 +++- 10 files changed, 3599 insertions(+), 58 deletions(-) create mode 100644 theories/lang_syntax.v create mode 100644 theories/lang_syntax_examples.v create mode 100644 theories/lang_syntax_toy.v create mode 100644 theories/lang_syntax_util.v diff --git a/_CoqProject b/_CoqProject index bf2b97069..bfb8be0be 100644 --- a/_CoqProject +++ b/_CoqProject @@ -95,3 +95,7 @@ analysis_stdlib/Rstruct_topology.v analysis_stdlib/showcase/uniform_bigO.v theories/prob_lang.v theories/prob_lang_wip.v +theories/lang_syntax_util.v +theories/lang_syntax_toy.v +theories/lang_syntax.v +theories/lang_syntax_examples.v diff --git a/coq-mathcomp-analysis.opam b/coq-mathcomp-analysis.opam index a0890eb50..b56eb46df 100644 --- a/coq-mathcomp-analysis.opam +++ b/coq-mathcomp-analysis.opam @@ -19,6 +19,7 @@ depends: [ "coq-mathcomp-solvable" "coq-mathcomp-field" "coq-mathcomp-bigenough" { (>= "1.0.0") } + "coq-mathcomp-algebra-tactics" { (>= "1.1.1") } ] tags: [ diff --git a/theories/Make b/theories/Make index 4167837f0..bfe010956 100644 --- a/theories/Make +++ b/theories/Make @@ -61,3 +61,7 @@ all_analysis.v showcase/summability.v prob_lang.v prob_lang_wip.v +lang_syntax_util.v +lang_syntax_toy.v +lang_syntax.v +lang_syntax_examples.v diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v new file mode 100644 index 000000000..f2c305e7c --- /dev/null +++ b/theories/lang_syntax.v @@ -0,0 +1,1119 @@ +Require Import String. +From HB Require Import structures. +From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. +From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. +From mathcomp.classical Require Import functions cardinality fsbigop. +Require Import signed reals ereal topology normedtype sequences esum measure. +Require Import lebesgue_measure numfun lebesgue_integral kernel prob_lang. +Require Import lang_syntax_util. +From mathcomp Require Import ring lra. + +(******************************************************************************) +(* Syntax and Evaluation for a Probabilistic Programming Language *) +(* *) +(* typ == syntax for types of data structures *) +(* measurable_of_typ t == the measurable type corresponding to type t *) +(* It is of type {d & measurableType d} *) +(* mtyp_disp t == the display corresponding to type t *) +(* mtyp t == the measurable type corresponding to type t *) +(* It is of type measurableType (mtyp_disp t) *) +(* measurable_of_seq s == the product space corresponding to the *) +(* list s : seq typ *) +(* It is of type {d & measurableType d} *) +(* acc_typ s n == function that access the nth element of s : seq typ *) +(* It is a function from projT2 (measurable_of_seq s) *) +(* to projT2 (measurable_of_typ (nth Unit s n)) *) +(* ctx == type of context *) +(* := seq (string * type) *) +(* mctx_disp g == the display corresponding to the context g *) +(* mctx g := the measurable type corresponding to the context g *) +(* It is formed of nested pairings of measurable *) +(* spaces. It is of type measurableType (mctx_disp g) *) +(* flag == a flag is either D (deterministic) or *) +(* P (probabilistic) *) +(* exp f g t == syntax of expressions with flag f of type t *) +(* context g *) +(* dval R g t == "deterministic value", i.e., *) +(* function from mctx g to mtyp t *) +(* pval R g t == "probabilistic value", i.e., *) +(* s-finite kernel, from mctx g to mtyp t *) +(* mkswap k == given a kernel k : (Y * X) ~> Z, *) +(* returns a kernel of type (X * Y) ~> Z *) +(* letin' := mkcomp \o mkswap *) +(* e -D> f ; mf == the evaluation of the deterministic expression e *) +(* leads to the deterministic value f *) +(* (mf is the proof that f is measurable) *) +(* e -P> k == the evaluation of the probabilistic function f *) +(* leads to the probabilistic value k *) +(* execD e == a dependent pair of a function corresponding to the *) +(* evaluation of e and a proof that this function is *) +(* measurable *) +(* execP e == a s-finite kernel corresponding to the evaluation *) +(* of the probabilistic expression e *) +(* *) +(******************************************************************************) + +Set Implicit Arguments. +Unset Strict Implicit. +Unset Printing Implicit Defensive. + +Import Order.TTheory GRing.Theory Num.Def Num.Theory. +Import numFieldTopology.Exports. + +Reserved Notation "f .; g" (at level 60, right associativity, + format "f .; '/ ' g"). +Reserved Notation "e -D> f ; mf" (at level 40). +Reserved Notation "e -P> k" (at level 40). + +Local Open Scope classical_set_scope. +Local Open Scope ring_scope. +Local Open Scope ereal_scope. + +(* TODO: mv *) +Arguments measurable_fst {d1 d2 T1 T2}. +Arguments measurable_snd {d1 d2 T1 T2}. + +Section mswap. +Context d d' d3 (X : measurableType d) (Y : measurableType d') + (Z : measurableType d3) (R : realType). +Variable k : R.-ker Y * X ~> Z. + +Definition mswap xy U := k (swap xy) U. + +Let mswap0 xy : mswap xy set0 = 0. +Proof. done. Qed. + +Let mswap_ge0 x U : 0 <= mswap x U. +Proof. done. Qed. + +Let mswap_sigma_additive x : semi_sigma_additive (mswap x). +Proof. exact: measure_semi_sigma_additive. Qed. + +HB.instance Definition _ x := isMeasure.Build _ R _ + (mswap x) (mswap0 x) (mswap_ge0 x) (@mswap_sigma_additive x). + +Definition mkswap : _ -> {measure set Z -> \bar R} := + fun x => mswap x. + +Let measurable_fun_kswap U : + measurable U -> measurable_fun setT (mkswap ^~ U). +Proof. +move=> mU. +rewrite [X in measurable_fun _ X](_ : _ = k ^~ U \o @swap _ _)//. +apply measurableT_comp => //=; first exact: measurable_kernel. +exact: measurable_swap. +Qed. + +HB.instance Definition _ := isKernel.Build _ _ + (X * Y)%type Z R mkswap measurable_fun_kswap. + +End mswap. + +Section mswap_sfinite_kernel. +Variables (d d' d3 : _) (X : measurableType d) (Y : measurableType d') + (Z : measurableType d3) (R : realType). +Variable k : R.-sfker Y * X ~> Z. + +Let mkswap_sfinite : + exists2 k_ : (R.-ker X * Y ~> Z)^nat, + forall n, measure_fam_uub (k_ n) & + forall x U, measurable U -> mkswap k x U = kseries k_ x U. +Proof. +have [k_ /= kE] := sfinite_kernel k. +exists (fun n => mkswap (k_ n)). + move=> n. + have /measure_fam_uubP[M hM] := measure_uub (k_ n). + by exists M%:num => x/=; exact: hM. +move=> xy U mU. +by rewrite /mswap/= kE. +Qed. + +HB.instance Definition _ := + Kernel_isSFinite_subdef.Build _ _ _ Z R (mkswap k) mkswap_sfinite. + +End mswap_sfinite_kernel. + +Section kswap_finite_kernel_finite. +Context d d' d3 (X : measurableType d) (Y : measurableType d') + (Z : measurableType d3) (R : realType) + (k : R.-fker Y * X ~> Z). + +Let mkswap_finite : measure_fam_uub (mkswap k). +Proof. +have /measure_fam_uubP[r hr] := measure_uub k. +apply/measure_fam_uubP; exists (PosNum [gt0 of r%:num%R]) => x /=. +exact: hr. +Qed. + +HB.instance Definition _ := + Kernel_isFinite.Build _ _ _ Z R (mkswap k) mkswap_finite. + +End kswap_finite_kernel_finite. + +Notation "l .; k" := (mkcomp l (mkswap k)) : ereal_scope. + +Section letin'. +Variables (d d' d3 : _) (X : measurableType d) (Y : measurableType d') + (Z : measurableType d3) (R : realType). + +Definition letin' (l : R.-sfker X ~> Y) (k : R.-sfker Y * X ~> Z) := + locked [the R.-sfker X ~> Z of l .; k]. + +Lemma letin'E (l : R.-sfker X ~> Y) (k : R.-sfker Y * X ~> Z) x U : + letin' l k x U = \int[l x]_y k (y, x) U. +Proof. by rewrite /letin'; unlock. Qed. + +Lemma letin'_letin (l : R.-sfker X ~> Y) (k : R.-sfker Y * X ~> Z) : + letin' l k = letin l (mkswap k). +Proof. by rewrite /letin'; unlock. Qed. + +End letin'. + +Section letin'C. +Import Notations. +Context d d1 d' (X : measurableType d) (Y : measurableType d1) + (Z : measurableType d') (R : realType). +Variables (t : R.-sfker Z ~> X) + (u' : R.-sfker X * Z ~> Y) + (u : R.-sfker Z ~> Y) + (t' : R.-sfker Y * Z ~> X) + (tt' : forall y, t =1 fun z => t' (y, z)) + (uu' : forall x, u =1 fun z => u' (x, z)). + +Definition T' z : set X -> \bar R := t z. +Let T0 z : (T' z) set0 = 0. Proof. by []. Qed. +Let T_ge0 z x : 0 <= (T' z) x. Proof. by []. Qed. +Let T_semi_sigma_additive z : semi_sigma_additive (T' z). +Proof. exact: measure_semi_sigma_additive. Qed. +HB.instance Definition _ z := @isMeasure.Build _ R X (T' z) (T0 z) (T_ge0 z) + (@T_semi_sigma_additive z). + +Let sfinT z : sfinite_measure (T' z). Proof. exact: sfinite_kernel_measure. Qed. +HB.instance Definition _ z := @Measure_isSFinite_subdef.Build _ X R + (T' z) (sfinT z). + +Definition U' z : set Y -> \bar R := u z. +Let U0 z : (U' z) set0 = 0. Proof. by []. Qed. +Let U_ge0 z x : 0 <= (U' z) x. Proof. by []. Qed. +Let U_semi_sigma_additive z : semi_sigma_additive (U' z). +Proof. exact: measure_semi_sigma_additive. Qed. +HB.instance Definition _ z := @isMeasure.Build _ R Y (U' z) (U0 z) (U_ge0 z) + (@U_semi_sigma_additive z). + +Let sfinU z : sfinite_measure (U' z). Proof. exact: sfinite_kernel_measure. Qed. +HB.instance Definition _ z := @Measure_isSFinite_subdef.Build _ Y R + (U' z) (sfinU z). + +Lemma letin'C z A : measurable A -> + letin' t + (letin' u' + (ret (measurable_fun_prod macc1of3' macc0of3'))) z A = + letin' u + (letin' t' + (ret (measurable_fun_prod macc0of3' macc1of3'))) z A. +Proof. +move=> mA. +rewrite !letin'E. +under eq_integral. + move=> x _. + rewrite letin'E -uu'. + under eq_integral do rewrite retE /=. + over. +rewrite (sfinite_Fubini (T' z) (U' z) (fun x => \d_(x.1, x.2) A ))//; last first. + apply/EFin_measurable_fun => /=; rewrite (_ : (fun x => _) = mindic R mA)//. + by apply/funext => -[]. +rewrite /=. +apply: eq_integral => y _. +by rewrite letin'E/= -tt'; apply: eq_integral => // x _; rewrite retE. +Qed. + +End letin'C. +Arguments letin'C {d d1 d' X Y Z R} _ _ _ _. + +Section letin'A. +Context d d' d1 d2 d3 (X : measurableType d) (Y : measurableType d') + (T1 : measurableType d1) (T2 : measurableType d2) (T3 : measurableType d3) + (R : realType). +Import Notations. +Variables (t : R.-sfker X ~> T1) + (u : R.-sfker T1 * X ~> T2) + (v : R.-sfker T2 * X ~> Y) + (v' : R.-sfker T2 * (T1 * X) ~> Y) + (vv' : forall y, v =1 fun xz => v' (xz.1, (y, xz.2))). + +Lemma letin'A x A : measurable A -> + letin' t (letin' u v') x A + = + (letin' (letin' t u) v) x A. +Proof. +move=> mA. +rewrite !letin'E. +under eq_integral do rewrite letin'E. +rewrite letin'_letin/=. +rewrite integral_kcomp; [|by []|]. + apply: eq_integral => z _. + apply: eq_integral => y _. + by rewrite (vv' z). +exact: measurableT_comp (@measurable_kernel _ _ _ _ _ v _ mA) _. +Qed. + +End letin'A. + +Declare Scope lang_scope. +Delimit Scope lang_scope with P. + +Section syntax_of_types. +Import Notations. +Context {R : realType}. + +Inductive typ := +| Unit | Bool | Real +| Pair : typ -> typ -> typ +| Prob : typ -> typ. + +Canonical stype_eqType := Equality.Pack (@gen_eqMixin typ). + +Fixpoint measurable_of_typ (t : typ) : {d & measurableType d} := + match t with + | Unit => existT _ _ munit + | Bool => existT _ _ mbool + | Real => existT _ _ (mR R) + | Pair A B => existT _ _ + [the measurableType (projT1 (measurable_of_typ A), + projT1 (measurable_of_typ B)).-prod%mdisp of + (projT2 (measurable_of_typ A) * + projT2 (measurable_of_typ B))%type] + | Prob A => existT _ _ (pprobability (projT2 (measurable_of_typ A)) R) + end. + +Definition mtyp_disp t : measure_display := projT1 (measurable_of_typ t). + +Definition mtyp t : measurableType (mtyp_disp t) := + projT2 (measurable_of_typ t). + +Definition measurable_of_seq (l : seq typ) : {d & measurableType d} := + iter_mprod (map measurable_of_typ l). + +End syntax_of_types. +Arguments measurable_of_typ {R}. +Arguments mtyp {R}. +Arguments measurable_of_seq {R}. + +Section accessor_functions. +Context {R : realType}. + +(* NB: almost the same as acc (map (@measurable_of_typ R) s) n l, + modulo commutativity of map and measurable_of_typ *) +Fixpoint acc_typ (s : seq typ) n : + projT2 (@measurable_of_seq R s) -> + projT2 (measurable_of_typ (nth Unit s n)) := + match s return + projT2 (measurable_of_seq s) -> projT2 (measurable_of_typ (nth Unit s n)) + with + | [::] => match n with | 0 => (fun=> tt) | m.+1 => (fun=> tt) end + | a :: l => match n with + | 0 => fst + | m.+1 => fun H => @acc_typ l m H.2 + end + end. + +(*Definition acc_typ : forall (s : seq typ) n, + projT2 (@measurable_of_seq R s) -> + projT2 (@measurable_of_typ R (nth Unit s n)). +fix H 1. +intros s n x. +destruct s as [|s]. + destruct n as [|n]. + exact tt. + exact tt. +destruct n as [|n]. + exact (fst x). +rewrite /=. +apply H. +exact: (snd x). +Show Proof. +Defined.*) + +Lemma measurable_acc_typ (s : seq typ) n : measurable_fun setT (@acc_typ s n). +Proof. +elim: s n => //= h t ih [|m]; first exact: measurable_fst. +by apply: (measurableT_comp (ih _)); exact: measurable_snd. +Qed. + +End accessor_functions. +Arguments acc_typ {R} s n. +Arguments measurable_acc_typ {R} s n. + +Section context. +Variables (R : realType). +Definition ctx := seq (string * typ). + +Definition mctx_disp (g : ctx) := projT1 (@measurable_of_seq R (map snd g)). + +Definition mctx (g : ctx) : measurableType (mctx_disp g) := + projT2 (@measurable_of_seq R (map snd g)). + +End context. +Arguments mctx {R}. + +Section syntax_of_expressions. +Context {R : realType}. + +Inductive flag := D | P. + +Inductive exp : flag -> ctx -> typ -> Type := +| exp_unit g : exp D g Unit +| exp_bool g : bool -> exp D g Bool +| exp_real g : R -> exp D g Real +| exp_pair g t1 t2 : exp D g t1 -> exp D g t2 -> exp D g (Pair t1 t2) +| exp_proj1 g t1 t2 : exp D g (Pair t1 t2) -> exp D g t1 +| exp_proj2 g t1 t2 : exp D g (Pair t1 t2) -> exp D g t2 +| exp_var g str t : t = lookup Unit g str -> exp D g t +| exp_bernoulli g (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : + exp D g (Prob Bool) +| exp_poisson g : nat -> exp D g Real -> exp D g Real +| exp_normalize g t : exp P g t -> exp D g (Prob t) +| exp_letin g t1 t2 str : exp P g t1 -> exp P ((str, t1) :: g) t2 -> + exp P g t2 +| exp_sample g t : exp D g (Prob t) -> exp P g t +| exp_score g : exp D g Real -> exp P g Unit +| exp_return g t : exp D g t -> exp P g t +| exp_if z g t : exp D g Bool -> exp z g t -> exp z g t -> exp z g t +| exp_weak z g h t x : exp z (g ++ h) t -> + x.1 \notin dom (g ++ h) -> exp z (g ++ x :: h) t. +Arguments exp_var {g} _ {t}. + +Definition exp_var' (str : string) (t : typ) (g : find str t) := + @exp_var (untag (ctx_of g)) str t (ctx_prf g). +Arguments exp_var' str {t} g. + +Lemma exp_var'E str t (f : find str t) H : + exp_var' str f = exp_var str H :> (@exp _ _ _). +Proof. by rewrite /exp_var'; congr exp_var. Qed. + +End syntax_of_expressions. +Arguments exp {R}. +Arguments exp_unit {R g}. +Arguments exp_bool {R g}. +Arguments exp_real {R g}. +Arguments exp_pair {R g} & {t1 t2}. +Arguments exp_var {R g} _ {t} H. +Arguments exp_bernoulli {R g}. +Arguments exp_poisson {R g}. +Arguments exp_normalize {R g _}. +Arguments exp_letin {R g} & {_ _}. +Arguments exp_sample {R g t}. +Arguments exp_score {R g}. +Arguments exp_return {R g} & {_}. +Arguments exp_if {R z g t}. +Arguments exp_weak {R} z g h {t} x. +Arguments exp_var' {R} str {t} g. + +Declare Custom Entry expr. +Notation "[ e ]" := e (e custom expr at level 5) : lang_scope. +Notation "'TT'" := (exp_unit) (in custom expr at level 1) : lang_scope. +Notation "b ':B'" := (@exp_bool _ _ b%bool) + (in custom expr at level 1) : lang_scope. +Notation "r ':R'" := (@exp_real _ _ r%R) + (in custom expr at level 1, format "r :R") : lang_scope. +Notation "'return' e" := (@exp_return _ _ _ e) + (in custom expr at level 2) : lang_scope. +(*Notation "% str" := (@exp_var _ _ str%string _ erefl) + (in custom expr at level 1, format "% str") : lang_scope.*) +(* Notation "% str H" := (@exp_var _ _ str%string _ H) + (in custom expr at level 1, format "% str H") : lang_scope. *) +Notation "# str" := (@exp_var' _ str%string _ _) + (in custom expr at level 1, format "# str"). +Notation "e :+ str" := (exp_weak _ [::] _ (str, _) e erefl) + (in custom expr at level 1) : lang_scope. +Notation "( e1 , e2 )" := (exp_pair e1 e2) + (in custom expr at level 1) : lang_scope. +Notation "\pi_1 e" := (exp_proj1 e) + (in custom expr at level 1) : lang_scope. +Notation "\pi_2 e" := (exp_proj2 e) + (in custom expr at level 1) : lang_scope. +Notation "'let' x ':=' e 'in' f" := (exp_letin x e f) + (in custom expr at level 3, + x constr, + f custom expr at level 3, + left associativity) : lang_scope. +Notation "{ c }" := c (in custom expr, c constr) : lang_scope. +Notation "x" := x + (in custom expr at level 0, x ident) : lang_scope. +Notation "'Sample' e" := (exp_sample e) + (in custom expr at level 2) : lang_scope. +Notation "'Score' e" := (exp_score e) + (in custom expr at level 2) : lang_scope. +Notation "'Normalize' e" := (exp_normalize e) + (in custom expr at level 0) : lang_scope. +Notation "'if' e1 'then' e2 'else' e3" := (exp_if e1 e2 e3) + (in custom expr at level 1) : lang_scope. + +Local Open Scope lang_scope. +Example three_letin {R : realType} x y z : @exp R P [::] _ := + [let x := return {1}:R in + let y := return #x in + let z := return #y in return #z]. + +Section free_vars. +Context {R : realType}. + +Fixpoint free_vars k g t (e : @exp R k g t) : seq string := + match e with + | exp_unit _ => [::] + | exp_bool _ _ => [::] + | exp_real _ _ => [::] + | exp_pair _ _ _ e1 e2 => free_vars e1 ++ free_vars e2 + | exp_proj1 _ _ _ e => free_vars e + | exp_proj2 _ _ _ e => free_vars e + | exp_var _ x _ _ => [:: x] + | exp_bernoulli _ _ _ => [::] + | exp_poisson _ _ e => free_vars e + | exp_normalize _ _ e => free_vars e + | exp_letin _ _ _ x e1 e2 => free_vars e1 ++ rem x (free_vars e2) + | exp_sample _ _ _ => [::] + | exp_score _ e => free_vars e + | exp_return _ _ e => free_vars e + | exp_if _ _ _ e1 e2 e3 => free_vars e1 ++ free_vars e2 ++ free_vars e3 + | exp_weak _ _ _ _ x e _ => rem x.1 (free_vars e) + end. + +End free_vars. + +Definition dval R g t := @mctx R g -> @mtyp R t. +Definition pval R g t := R.-sfker @mctx R g ~> @mtyp R t. + +Section weak. +Context {R : realType}. +Implicit Types (g h : ctx) (x : string * typ). + +Fixpoint mctx_strong g h x (f : @mctx R (g ++ x :: h)) : @mctx R (g ++ h) := + match g as g0 return mctx (g0 ++ x :: h) -> mctx (g0 ++ h) with + | [::] => fun f0 : mctx ([::] ++ x :: h) => let (a, b) := f0 in (fun=> id) a b + | a :: t => uncurry (fun a b => (a, @mctx_strong t h x b)) + end f. + +Definition weak g h x t (f : dval R (g ++ h) t) : dval R (g ++ x :: h) t := + f \o @mctx_strong g h x. + +Lemma measurable_fun_mctx_strong g h x : + measurable_fun setT (@mctx_strong g h x). +Proof. +elim: g h x => [h x|x g ih h x0]; first exact: measurable_snd. +apply/prod_measurable_funP; split. +- rewrite [X in measurable_fun _ X](_ : _ = fst)//. + by apply/funext => -[]. +- rewrite [X in measurable_fun _ X](_ : _ = @mctx_strong g h x0 \o snd). + apply: measurableT_comp; last exact: measurable_snd. + exact: ih. + by apply/funext => -[]. +Qed. + +Lemma measurable_weak g h x t (f : dval R (g ++ h) t) : + measurable_fun setT f -> measurable_fun setT (@weak g h x t f). +Proof. +move=> mf; apply: measurableT_comp; first exact: mf. +exact: measurable_fun_mctx_strong. +Qed. + +Definition kweak g h x t (f : pval R (g ++ h) t) + : @mctx R (g ++ x :: h) -> {measure set @mtyp R t -> \bar R} := + f \o @mctx_strong g h x. + +Section kernel_weak. +Context g h x t (f : pval R (g ++ h) t). + +Let mf U : measurable U -> measurable_fun setT (@kweak g h x t f ^~ U). +Proof. +move=> mU. +rewrite (_ : kweak _ ^~ U = f ^~ U \o @mctx_strong g h x)//. +apply: measurableT_comp => //; first exact: measurable_kernel. +exact: measurable_fun_mctx_strong. +Qed. + +HB.instance Definition _ := isKernel.Build _ _ _ _ _ (@kweak g h x t f) mf. +End kernel_weak. + +Section sfkernel_weak. +Context g h (x : string * typ) t (f : pval R (g ++ h) t). + +Let sf : exists2 s : (R.-ker @mctx R (g ++ x :: h) ~> @mtyp R t)^nat, + forall n, measure_fam_uub (s n) & + forall z U, measurable U -> (@kweak g h x t f) z U = kseries s z U . +Proof. +have [s hs] := sfinite_kernel f. +exists (fun n => @kweak g h x t (s n)). + by move=> n; have [M hM] := measure_uub (s n); exists M => x0; exact: hM. +by move=> z U mU; by rewrite /kweak/= hs. +Qed. + +HB.instance Definition _ := + Kernel_isSFinite_subdef.Build _ _ _ _ _ (@kweak g h x t f) sf. + +End sfkernel_weak. + +Section fkernel_weak. +Context g h x t (f : R.-fker @mctx R (g ++ h) ~> @mtyp R t). + +Let uub : measure_fam_uub (@kweak g h x t f). +Proof. by have [M hM] := measure_uub f; exists M => x0; exact: hM. Qed. + +HB.instance Definition _ := @Kernel_isFinite.Build _ _ _ _ _ + (@kweak g h x t f) uub. +End fkernel_weak. + +End weak. +Arguments weak {R} g h x {t}. +Arguments measurable_weak {R} g h x {t}. +Arguments kweak {R} g h x {t}. + +Section eval. +Context {R : realType}. +Implicit Type (g : ctx) (str : string). +Local Open Scope lang_scope. + +Inductive evalD : forall g t, exp D g t -> + forall f : dval R g t, measurable_fun setT f -> Prop := +| eval_unit g : ([TT] : exp D g _) -D> cst tt ; ktt + +| eval_bool g b : ([b:B] : exp D g _) -D> cst b ; kb b + +| eval_real g r : ([r:R] : exp D g _) -D> cst r ; kr r + +| eval_pair g t1 (e1 : exp D g t1) f1 mf1 t2 (e2 : exp D g t2) f2 mf2 : + e1 -D> f1 ; mf1 -> e2 -D> f2 ; mf2 -> + [(e1, e2)] -D> fun x => (f1 x, f2 x) ; measurable_fun_prod mf1 mf2 + +| eval_proj1 g t1 t2 (e : exp D g (Pair t1 t2)) f mf : + e -D> f ; mf -> + [\pi_1 e] -D> fst \o f ; measurableT_comp measurable_fst mf + +| eval_proj2 g t1 t2 (e : exp D g (Pair t1 t2)) f mf : + e -D> f ; mf -> + [\pi_2 e] -D> snd \o f ; measurableT_comp measurable_snd mf + +(* | eval_var g str : let i := index str (dom g) in + [% str] -D> acc_typ (map snd g) i ; measurable_acc_typ (map snd g) i *) + +| eval_var g x H : let i := index x (dom g) in + exp_var x H -D> acc_typ (map snd g) i ; measurable_acc_typ (map snd g) i + +| eval_bernoulli g (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : + (exp_bernoulli r r1 : exp D g _) -D> cst (bernoulli r1) ; + measurable_cst _ + +| eval_poisson g n (e : exp D g _) f mf : + e -D> f ; mf -> + exp_poisson n e -D> poisson n \o f ; + measurableT_comp (measurable_poisson n) mf + +| eval_normalize g t (e : exp P g t) k : + e -P> k -> + [Normalize e] -D> normalize k point ; measurable_fun_mnormalize k + +| evalD_if g t e f mf (e1 : exp D g t) f1 mf1 e2 f2 mf2 : + e -D> f ; mf -> e1 -D> f1 ; mf1 -> e2 -D> f2 ; mf2 -> + [if e then e1 else e2] -D> fun x => if f x then f1 x else f2 x ; + measurable_fun_ifT mf mf1 mf2 + +| evalD_weak g h t e x (H : x.1 \notin dom (g ++ h)) f mf : + e -D> f ; mf -> + (exp_weak _ g h x e H : exp _ _ t) -D> weak g h x f ; + measurable_weak g h x f mf + +where "e -D> v ; mv" := (@evalD _ _ e v mv) + +with evalP : forall g t, exp P g t -> pval R g t -> Prop := + +| eval_letin g t1 t2 str (e1 : exp _ g t1) (e2 : exp _ _ t2) k1 k2 : + e1 -P> k1 -> e2 -P> k2 -> + [let str := e1 in e2] -P> letin' k1 k2 + +| eval_sample g t (e : exp _ _ (Prob t)) + (p : mctx g -> pprobability (mtyp t) R) mp : + e -D> p ; mp -> [Sample e] -P> sample p mp + +| eval_score g (e : exp _ g _) f mf : + e -D> f ; mf -> [Score e] -P> kscore mf + +| eval_return g t (e : exp D g t) f mf : + e -D> f ; mf -> [return e] -P> ret mf + +| evalP_if g t e f mf (e1 : exp P g t) k1 e2 k2 : + e -D> f ; mf -> e1 -P> k1 -> e2 -P> k2 -> + [if e then e1 else e2] -P> ite mf k1 k2 + +| evalP_weak g h t (e : exp P (g ++ h) t) x + (H : x.1 \notin dom (g ++ h)) f : + e -P> f -> + exp_weak _ g h x e H -P> kweak g h x f + +where "e -P> v" := (@evalP _ _ e v). + +End eval. + +Notation "e -D> v ; mv" := (@evalD _ _ _ e v mv) : lang_scope. +Notation "e -P> v" := (@evalP _ _ _ e v) : lang_scope. + +Scheme evalD_mut_ind := Induction for evalD Sort Prop +with evalP_mut_ind := Induction for evalP Sort Prop. + +(* properties of the evaluation relation *) +Section eval_prop. +Variables (R : realType). +Local Open Scope lang_scope. + +Lemma evalD_uniq g t (e : exp D g t) (u v : dval R g t) mu mv : + e -D> u ; mu -> e -D> v ; mv -> u = v. +Proof. +move=> hu. +apply: (@evalD_mut_ind R + (fun g t (e : exp D g t) f mf (h1 : e -D> f; mf) => + forall v mv, e -D> v; mv -> f = v) + (fun g t (e : exp P g t) u (h1 : e -P> u) => + forall v, e -P> v -> u = v)); last exact: hu. +all: (rewrite {g t e u v mu mv hu}). +- move=> g {}v {}mv. + inversion 1; subst g0. + by inj_ex H3. +- move=> g b {}v {}mv. + inversion 1; subst g0 b0. + by inj_ex H3. +- move=> g r {}v {}mv. + inversion 1; subst g0 r0. + by inj_ex H3. +- move=> g t1 e1 f1 mf1 t2 e2 f2 mf2 ev1 IH1 ev2 IH2 {}v {}mv. + simple inversion 1 => //; subst g0. + case: H3 => ? ?; subst t0 t3. + inj_ex H4; case: H4 => He1 He2. + inj_ex He1; subst e0. + inj_ex He2; subst e3. + inj_ex H5; subst v. + by move=> /IH1 <- /IH2 <-. +- move=> g t1 t2 e f mf H ih v mv. + inversion 1; subst g0 t3 t0. + inj_ex H11; subst v. + clear H9. + inj_ex H7; subst e1. + by rewrite (ih _ _ H4). +- move=> g t1 t2 e f mf H ih v mv. + inversion 1; subst g0 t3 t0. + inj_ex H11; subst v. + clear H9. + inj_ex H7; subst e1. + by rewrite (ih _ _ H4). +(* - move=> g str n {}v {}mv. + inversion 1; subst g0. + inj_ex H6; rewrite -H6. + by inj_ex H7. + inj_ex H8; rewrite -H8. + by inj_ex H9. *) +- move=> g str H n {}v {}mv. + inversion 1; subst g0. + (* inj_ex H7; rewrite -H7. + by inj_ex H8. *) + inj_ex H9; rewrite -H9. + by inj_ex H10. +- move=> g r r1 {}v {}mv. + inversion 1; subst g0 r0. + inj_ex H3; subst v. + by have -> : r1 = r3 by []. +- move=> g n e0 f mf ev IH {}v {}mv. + inversion 1; subst g0 n0. + inj_ex H2; subst e0. + inj_ex H4; subst v. + by rewrite (IH _ _ H3). +- move=> g t e0 k ev IH {}v {}mv. + inversion 1; subst g0 t0. + inj_ex H2; subst e0. + inj_ex H4; subst v. + by rewrite (IH _ H3). +- move=> g t e f mf e1 f1 mf1 e2 f2 mf2 ev ih ev1 ih1 ev2 ih2 v m. + inversion 1; subst g0 t0. + inj_ex H2; subst e0. + inj_ex H6; subst e5. + inj_ex H7; subst e6. + inj_ex H9; subst v. + clear H11. + have ? := ih1 _ _ H12; subst f6. + have ? := ih2 _ _ H13; subst f7. + by rewrite (ih _ _ H5). +- move=> g h t e x H f mf ef ih {}v {}mv. + inversion 1; subst t0 g0 h0 x0. + inj_ex H12; subst e1. + inj_ex H14; subst v. + clear H16. + by rewrite (ih _ _ H5). +- move=> g t1 t2 x e1 e2 k1 k2 ev1 IH1 ev2 IH2 k. + inversion 1; subst g0 t0 t3 x. + inj_ex H7; subst k. + inj_ex H6; subst e5. + inj_ex H5; subst e4. + by rewrite (IH1 _ H4) (IH2 _ H8). +- move=> g t e p mp ev IH k. + inversion 1; subst g0. + inj_ex H5; subst t0. + inj_ex H5; subst e1. + inj_ex H7; subst k. + have ? := IH _ _ H3; subst p1. + by have -> : mp = mp1 by []. +- move=> g e f mf ev IH k. + inversion 1; subst g0. + inj_ex H0; subst e0. + inj_ex H4; subst k. + have ? := IH _ _ H2; subst f1. + by have -> : mf = mf0 by []. +- move=> g t e0 f mf ev IH k. + inversion 1; subst g0 t0. + inj_ex H5; subst e1. + inj_ex H7; subst k. + have ? := IH _ _ H3; subst f1. + by have -> : mf = mf1 by []. +- move=> g t e f mf e1 k1 e2 k2 ev ih ev1 ih1 ev2 ih2 k. + inversion 1; subst g0 t0. + inj_ex H0; subst e0. + inj_ex H1; subst e3. + inj_ex H5; subst k. + inj_ex H2; subst e4. + have ? := ih _ _ H6; subst f1. + have -> : mf = mf0 by []. + by rewrite (ih1 _ H7) (ih2 _ H8). +- move=> g h t e x xgh k ek ih. + inversion 1; subst x0 g0 h0 t0. + inj_ex H13; rewrite -H13. + inj_ex H11; subst e1. + by rewrite (ih _ H4). +Qed. + +Lemma evalP_uniq g t (e : exp P g t) (u v : pval R g t) : + e -P> u -> e -P> v -> u = v. +Proof. +move=> eu. +apply: (@evalP_mut_ind R + (fun g t (e : exp D g t) f mf (h : e -D> f; mf) => + forall v mv, e -D> v; mv -> f = v) + (fun g t (e : exp P g t) u (h : e -P> u) => + forall v, e -P> v -> u = v)); last exact: eu. +all: rewrite {g t e u v eu}. +- move=> g {}v {}mv. + inversion 1; subst g0. + by inj_ex H3. +- move=> g b {}v {}mv. + inversion 1; subst g0 b0. + by inj_ex H3. +- move=> g r {}v {}mv. + inversion 1; subst g0 r0. + by inj_ex H3. +- move=> g t1 e1 f1 mf1 t2 e2 f2 mf2 ev1 IH1 ev2 IH2 {}v {}mv. + simple inversion 1 => //; subst g0. + case: H3 => ? ?; subst t0 t3. + inj_ex H4; case: H4 => He1 He2. + inj_ex He1; subst e0. + inj_ex He2; subst e3. + inj_ex H5; subst v. + move=> e1f0 e2f3. + by rewrite (IH1 _ _ e1f0) (IH2 _ _ e2f3). +- move=> g t1 t2 e f mf H ih v mv. + inversion 1; subst g0 t3 t0. + inj_ex H11; subst v. + clear H9. + inj_ex H7; subst e1. + by rewrite (ih _ _ H4). +- move=> g t1 t2 e f mf H ih v mv. + inversion 1; subst g0 t3 t0. + inj_ex H11; subst v. + clear H9. + inj_ex H7; subst e1. + by rewrite (ih _ _ H4). +(* - move=> g str n {}v {}mv. + inversion 1; subst g0. + inj_ex H6; rewrite -H6. + by inj_ex H7. + inj_ex H8; rewrite -H8. + by inj_ex H9. *) +- move=> g str H n {}v {}mv. + inversion 1; subst g0. + (* inj_ex H7; rewrite -H7. + by inj_ex H8. *) + inj_ex H9; rewrite -H9. + by inj_ex H10. +- move=> g r r1 {}v {}mv. + inversion 1; subst g0 r0. + inj_ex H3; subst v. + by have -> : r1 = r3 by []. +- move=> g n e f mf ev IH {}v {}mv. + inversion 1; subst g0 n0. + inj_ex H2; subst e0. + inj_ex H4; subst v. + inj_ex H5; subst mv. + by rewrite (IH _ _ H3). +- move=> g t e k ev IH {}v {}mv. + inversion 1; subst g0 t0. + inj_ex H2; subst e0. + inj_ex H4; subst v. + inj_ex H5; subst mv. + by rewrite (IH _ H3). +- move=> g t e f mf e1 f1 mf1 e2 f2 mf2 ef ih ef1 ih1 ef2 ih2 {}v {}mv. + inversion 1; subst g0 t0. + inj_ex H2; subst e0. + inj_ex H6; subst e5. + inj_ex H7; subst e6. + inj_ex H9; subst v. + clear H11. + have ? := ih1 _ _ H12; subst f6. + have ? := ih2 _ _ H13; subst f7. + by rewrite (ih _ _ H5). +- move=> g h t e x H f mf ef ih {}v {}mv. + inversion 1; subst x0 g0 h0 t0. + inj_ex H12; subst e1. + inj_ex H14; subst v. + clear H16. + by rewrite (ih _ _ H5). +- move=> g t1 t2 x e1 e2 k1 k2 ev1 IH1 ev2 IH2 k. + inversion 1; subst g0 x t3 t0. + inj_ex H7; subst k. + inj_ex H5; subst e4. + inj_ex H6; subst e5. + by rewrite (IH1 _ H4) (IH2 _ H8). +- move=> g t e p mp ep IH v. + inversion 1; subst g0 t0. + inj_ex H7; subst v. + inj_ex H5; subst e1. + have ? := IH _ _ H3; subst p1. + by have -> : mp = mp1 by []. +- move=> g e f mf ev IH k. + inversion 1; subst g0. + inj_ex H0; subst e0. + inj_ex H4; subst k. + have ? := IH _ _ H2; subst f1. + by have -> : mf = mf0 by []. +- move=> g t e f mf ev IH k. + inversion 1; subst g0 t0. + inj_ex H7; subst k. + inj_ex H5; subst e1. + have ? := IH _ _ H3; subst f1. + by have -> : mf = mf1 by []. +- move=> g t e f mf e1 k1 e2 k2 ev ih ev1 ih1 ev2 ih2 k. + inversion 1; subst g0 t0. + inj_ex H0; subst e0. + inj_ex H1; subst e3. + inj_ex H5; subst k. + inj_ex H2; subst e4. + have ? := ih _ _ H6; subst f1. + have -> : mf0 = mf by []. + by rewrite (ih1 _ H7) (ih2 _ H8). +- move=> g h t e x xgh k ek ih. + inversion 1; subst x0 g0 h0 t0. + inj_ex H13; rewrite -H13. + inj_ex H11; subst e1. + by rewrite (ih _ H4). +Qed. + +Lemma eval_total z g t (e : @exp R z g t) : + (match z with + | D => fun e => exists f mf, e -D> f ; mf + | P => fun e => exists k, e -P> k + end) e. +Proof. +elim: e. +all: rewrite {z g t}. +- by do 2 eexists; exact: eval_unit. +- by do 2 eexists; exact: eval_bool. +- by do 2 eexists; exact: eval_real. +- move=> g t1 t2 e1 [f1 [mf1 H1]] e2 [f2 [mf2 H2]]. + by exists (fun x => (f1 x, f2 x)); eexists; exact: eval_pair. +- move=> g t1 t2 e [f [mf H]]. + by exists (fst \o f); eexists; exact: eval_proj1. +- move=> g t1 t2 e [f [mf H]]. + by exists (snd \o f); eexists; exact: eval_proj2. +- by move=> g x t tE; subst t; eexists; eexists; exact: eval_var. +- by move=> r r1; eexists; eexists; exact: eval_bernoulli. +- move=> g h e [f [mf H]]. + by exists (poisson h \o f); eexists; exact: eval_poisson. +- move=> g t e [k ek]. + by exists (normalize k point); eexists; exact: eval_normalize. +- move=> g t1 t2 x e1 [k1 ev1] e2 [k2 ev2]. + by exists (letin' k1 k2); exact: eval_letin. +- move=> g t e [f [/= mf ef]]. + by eexists; exact: (@eval_sample _ _ _ _ _ mf). +- move=> g e [f [mf f_mf]]. + by exists (kscore mf); exact: eval_score. +- by move=> g t e [f [mf f_mf]]; exists (ret mf); exact: eval_return. +- case. + + move=> g t e1 [f [mf H1]] e2 [f2 [mf2 H2]] e3 [f3 [mf3 H3]]. + by exists (fun g => if f g then f2 g else f3 g), + (measurable_fun_ifT mf mf2 mf3); exact: evalD_if. + + move=> g t e1 [f [mf H1]] e2 [k2 H2] e3 [k3 H3]. + by exists (ite mf k2 k3); exact: evalP_if. +- case=> [g h t x e [f [mf ef]] xgh|g h st x e [k ek] xgh]. + + by exists (weak _ _ _ f), (measurable_weak _ _ _ _ mf); exact/evalD_weak. + + by exists (kweak _ _ _ k); exact: evalP_weak. +Qed. + +Lemma evalD_total g t (e : @exp R D g t) : exists f mf, e -D> f ; mf. +Proof. exact: (eval_total e). Qed. + +Lemma evalP_total g t (e : @exp R P g t) : exists k, e -P> k. +Proof. exact: (eval_total e). Qed. + +End eval_prop. + +Section execution_functions. +Local Open Scope lang_scope. +Context {R : realType}. +Implicit Type g : ctx. + +Definition execD g t (e : exp D g t) : + {f : dval R g t & measurable_fun setT f} := + let: exist _ H := cid (evalD_total e) in + existT _ _ (projT1 (cid H)). + +Lemma eq_execD g t (p1 p2 : @exp R D g t) : + projT1 (execD p1) = projT1 (execD p2) -> execD p1 = execD p2. +Proof. +rewrite /execD /=. +case: cid => /= f1 [mf1 ev1]. +case: cid => /= f2 [mf2 ev2] f12. +subst f2. +have ? : mf1 = mf2 by []. +subst mf2. +congr existT. +rewrite /sval. +case: cid => mf1' ev1'. +have ? : mf1 = mf1' by []. +subst mf1'. +case: cid => mf2' ev2'. +have ? : mf1 = mf2' by []. +by subst mf2'. +Qed. + +Definition execP g t (e : exp P g t) : pval R g t := + projT1 (cid (evalP_total e)). + +Lemma execD_evalD g t e x mx: + @execD g t e = existT _ x mx <-> e -D> x ; mx. +Proof. +rewrite /execD; split. + case: cid => x' [mx' H] [?]; subst x'. + have ? : mx = mx' by []. + by subst mx'. +case: cid => f' [mf' f'mf']/=. +move/evalD_uniq => /(_ _ _ f'mf') => ?; subst f'. +by case: cid => //= ? ?; congr existT. +Qed. + +Lemma evalD_execD g t (e : exp D g t) : + e -D> projT1 (execD e); projT2 (execD e). +Proof. +by rewrite /execD; case: cid => // x [mx xmx]/=; case: cid. +Qed. + +Lemma execP_evalP g t (e : exp P g t) x : + execP e = x <-> e -P> x. +Proof. +rewrite /execP; split; first by move=> <-; case: cid. +case: cid => // x0 Hx0. +by move/evalP_uniq => /(_ _ Hx0) ?; subst x. +Qed. + +Lemma evalP_execP g t (e : exp P g t) : e -P> execP e. +Proof. by rewrite /execP; case: cid. Qed. + +Lemma execD_unit g : @execD g _ [TT] = existT _ (cst tt) ktt. +Proof. exact/execD_evalD/eval_unit. Qed. + +Lemma execD_bool g b : @execD g _ [b:B] = existT _ (cst b) (kb b). +Proof. exact/execD_evalD/eval_bool. Qed. + +Lemma execD_real g r : @execD g _ [r:R] = existT _ (cst r) (kr r). +Proof. exact/execD_evalD/eval_real. Qed. + +Lemma execD_pair g t1 t2 (e1 : exp D g t1) (e2 : exp D g t2) : + let f1 := projT1 (execD e1) in let f2 := projT1 (execD e2) in + let mf1 := projT2 (execD e1) in let mf2 := projT2 (execD e2) in + execD [(e1, e2)] = + @existT _ _ (fun z => (f1 z, f2 z)) + (@measurable_fun_prod _ _ _ (mctx g) (mtyp t1) (mtyp t2) + f1 f2 mf1 mf2). +Proof. +by move=> f1 f2 mf1 mf2; apply/execD_evalD/eval_pair; exact: evalD_execD. +Qed. + +Lemma execD_proj1 g t1 t2 (e : exp D g (Pair t1 t2)) : + let f := projT1 (execD e) in + let mf := projT2 (execD e) in + execD [\pi_1 e] = @existT _ _ (fst \o f) + (measurableT_comp measurable_fst mf). +Proof. +by move=> f mf; apply/execD_evalD/eval_proj1; exact: evalD_execD. +Qed. + +Lemma execD_proj2 g t1 t2 (e : exp D g (Pair t1 t2)) : + let f := projT1 (execD e) in let mf := projT2 (execD e) in + execD [\pi_2 e] = @existT _ _ (snd \o f) + (measurableT_comp measurable_snd mf). +Proof. +by move=> f mf; apply/execD_evalD/eval_proj2; exact: evalD_execD. +Qed. + +Lemma execD_var_erefl g str : let i := index str (dom g) in + @execD g _ (exp_var str erefl) = existT _ (acc_typ (map snd g) i) + (measurable_acc_typ (map snd g) i). +Proof. by move=> i; apply/execD_evalD; exact: eval_var. Qed. + +Lemma execD_var g x (H : nth Unit (map snd g) (index x (dom g)) = lookup Unit g x) : + let i := index x (dom g) in + @execD g _ (exp_var x H) = existT _ (acc_typ (map snd g) i) + (measurable_acc_typ (map snd g) i). +Proof. by move=> i; apply/execD_evalD; exact: eval_var. Qed. + +Lemma execD_bernoulli g r (r1 : (r%:num <= 1)%R) : + @execD g _ (exp_bernoulli r r1) = + existT _ (cst [the probability _ _ of bernoulli r1]) (measurable_cst _). +Proof. exact/execD_evalD/eval_bernoulli. Qed. + +Lemma execD_normalize g t (e : exp P g t) : + @execD g _ [Normalize e] = + existT _ (normalize (execP e) point : _ -> pprobability _ _) + (measurable_fun_mnormalize (execP e)). +Proof. exact/execD_evalD/eval_normalize/evalP_execP. Qed. + +Lemma execD_poisson g n (e : exp D g Real) : + execD (exp_poisson n e) = + existT _ (poisson n \o (projT1 (execD e))) + (measurableT_comp (measurable_poisson n) (projT2 (execD e))). +Proof. exact/execD_evalD/eval_poisson/evalD_execD. Qed. + +Lemma execP_if g st e1 e2 e3 : + @execP g st [if e1 then e2 else e3] = + ite (projT2 (execD e1)) (execP e2) (execP e3). +Proof. +by apply/execP_evalP/evalP_if; [apply: evalD_execD| exact: evalP_execP..]. +Qed. + +Lemma execP_letin g x t1 t2 (e1 : exp P g t1) (e2 : exp P ((x, t1) :: g) t2) : + execP [let x := e1 in e2] = letin' (execP e1) (execP e2) :> (R.-sfker _ ~> _). +Proof. by apply/execP_evalP/eval_letin; exact: evalP_execP. Qed. + +Lemma execP_sample g t (e : @exp R D g (Prob t)) : + let x := execD e in + execP [Sample e] = sample (projT1 x) (projT2 x). +Proof. exact/execP_evalP/eval_sample/evalD_execD. Qed. + +Lemma execP_score g (e : exp D g Real) : + execP [Score e] = score (projT2 (execD e)). +Proof. exact/execP_evalP/eval_score/evalD_execD. Qed. + +Lemma execP_return g t (e : exp D g t) : + execP [return e] = ret (projT2 (execD e)). +Proof. exact/execP_evalP/eval_return/evalD_execD. Qed. + +Lemma execP_weak g h x t (e : exp P (g ++ h) t) + (xl : x.1 \notin dom (g ++ h)) : + execP (exp_weak P g h _ e xl) = kweak _ _ _ (execP e). +Proof. exact/execP_evalP/evalP_weak/evalP_execP. Qed. + +End execution_functions. +Arguments execD_var_erefl {R g} str. +Arguments execP_weak {R} g h x {t} e. +Arguments exp_var'E {R} str. diff --git a/theories/lang_syntax_examples.v b/theories/lang_syntax_examples.v new file mode 100644 index 000000000..98880bfd8 --- /dev/null +++ b/theories/lang_syntax_examples.v @@ -0,0 +1,784 @@ +Require Import String. +From HB Require Import structures. +From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. +From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. +From mathcomp.classical Require Import functions cardinality fsbigop. +Require Import signed reals ereal topology normedtype sequences esum measure. +Require Import lebesgue_measure numfun lebesgue_integral kernel prob_lang. +Require Import lang_syntax_util lang_syntax. +From mathcomp Require Import ring lra. + +(******************************************************************************) +(* Examples using the Probabilistic Programming Language of lang_syntax.v *) +(* *) +(* sample_pair_syntax := normalize ( *) +(* let x := sample (bernoulli 1/2) in *) +(* let y := sample (bernoulli 1/3) in *) +(* return (x, y)) *) +(* *) +(* bernoulli13_score := normalize ( *) +(* let x := sample (bernoulli 1/3) in *) +(* let _ := if x then score (1/3) else score (2/3) in *) +(* return x) *) +(* *) +(* bernoulli12_score := normalize ( *) +(* let x := sample (bernoulli 1/2) in *) +(* let _ := if x then score (1/3) else score (2/3) in *) +(* return x) *) +(* *) +(* hard_constraint := let x := Score {0}:R in return TT *) +(* *) +(* associativity of let-in expressions *) +(* *) +(* staton_bus_syntax == example from [Staton, ESOP 2017] *) +(* *) +(* staton_busA_syntax == same as staton_bus_syntax module associativity of *) +(* let-in expression *) +(* *) +(* commutativity of let-in expressions *) +(* *) +(******************************************************************************) + +Set Implicit Arguments. +Unset Strict Implicit. +Unset Printing Implicit Defensive. + +Import Order.TTheory GRing.Theory Num.Def Num.Theory. +Import numFieldTopology.Exports. + +Local Open Scope classical_set_scope. +Local Open Scope ring_scope. +Local Open Scope ereal_scope. + +(* letin' versions of rewriting laws *) +Lemma letin'_sample_bernoulli d d' (T : measurableType d) + (T' : measurableType d') (R : realType)(r : {nonneg R}) (r1 : (r%:num <= 1)%R) + (u : R.-sfker bool * T ~> T') x y : + letin' (sample_cst (bernoulli r1)) u x y = + r%:num%:E * u (true, x) y + (`1- (r%:num))%:E * u (false, x) y. +Proof. +rewrite letin'E/=. +rewrite ge0_integral_measure_sum// 2!big_ord_recl/= big_ord0 adde0/=. +by rewrite !ge0_integral_mscale//= !integral_dirac//= indicT 2!mul1e. +Qed. + +Section letin'_return. +Context d d' d3 (X : measurableType d) (Y : measurableType d') + (Z : measurableType d3) (R : realType). + +Lemma letin'_kret (k : R.-sfker X ~> Y) + (f : Y * X -> Z) (mf : measurable_fun setT f) x U : + measurable U -> + letin' k (ret mf) x U = k x (curry f ^~ x @^-1` U). +Proof. +move=> mU; rewrite letin'E. +under eq_integral do rewrite retE. +rewrite integral_indic ?setIT// -[X in measurable X]setTI. +exact: (measurableT_comp mf). +Qed. + +Lemma letin'_retk (f : X -> Y) (mf : measurable_fun setT f) + (k : R.-sfker Y * X ~> Z) x U : + measurable U -> letin' (ret mf) k x U = k (f x, x) U. +Proof. +move=> mU; rewrite letin'E retE integral_dirac ?indicT ?mul1e//. +exact: (measurableT_comp (measurable_kernel k _ mU)). +Qed. + +End letin'_return. + +Section letin'_ite. +Context d d2 d3 (T : measurableType d) (T2 : measurableType d2) + (Z : measurableType d3) (R : realType). +Variables (k1 k2 : R.-sfker T ~> Z) + (u : R.-sfker Z * T ~> T2) + (f : T -> bool) (mf : measurable_fun setT f) + (t : T) (U : set T2). + +Lemma letin'_iteT : f t -> letin' (ite mf k1 k2) u t U = letin' k1 u t U. +Proof. +move=> ftT. +rewrite !letin'E/=. +apply: eq_measure_integral => V mV _. +by rewrite iteE ftT. +Qed. + +Lemma letin'_iteF : ~~ f t -> letin' (ite mf k1 k2) u t U = letin' k2 u t U. +Proof. +move=> ftF. +rewrite !letin'E/=. +apply: eq_measure_integral => V mV _. +by rewrite iteE (negbTE ftF). +Qed. + +End letin'_ite. +(* /letin' versions of rewriting laws *) + +Local Open Scope lang_scope. + +Lemma execP_letinL {R : realType} g t1 t2 x (e1 : @exp R P g t1) (e1' : exp P g t1) + (e2 : exp P ((x, t1) :: g) t2) : + forall U, measurable U -> + execP e1 = execP e1' -> + execP [let x := e1 in e2] ^~ U = execP [let x := e1' in e2] ^~ U. +Proof. +by move=> U mU e1e1'; rewrite !execP_letin e1e1'. +Qed. + +Lemma execP_letinR {R : realType} g t1 t2 x (e1 : @exp R P g t1) + (e2 : exp P _ t2) (e2' : exp P ((x, t1) :: g) t2) : + forall U, measurable U -> + execP e2 = execP e2' -> + execP [let x := e1 in e2] ^~ U = execP [let x := e1 in e2'] ^~ U. +Proof. +by move=> U mU e1e1'; rewrite !execP_letin e1e1'. +Qed. + +Local Close Scope lang_scope. + +(* simple tests to check bidirectional hints *) +Module tests_bidi. +Local Open Scope lang_scope. +Import Notations. +Context (R : realType). + +Definition v1 x : @exp R P [::] _ := [ + let x := return {1}:R in + return #x]. + +Definition v2 (a b : string) + (a := "a") (b := "b") + (* (H : infer (b != a)) *) + : @exp R P [::] _ := [ + let a := return {1}:R in + let b := return {true}:B in + (* let c := return {3}:R in + let d := return {4}:R in *) + return (#a, #b)]. + +Definition v3 (a b c d : string) (H1 : infer (b != a)) (H2 : infer (c != a)) + (H3 : infer (c != b)) (H4 : infer (a != b)) (H5 : infer (a != c)) + (H6 : infer (b != c)) : @exp R P [::] _ := [ + let a := return {1}:R in + let b := return {2}:R in + let c := return {3}:R in + (* let d := return {4}:R in *) + return (#b, #a)]. + +End tests_bidi. + +Section trivial_example. +Local Open Scope lang_scope. +Import Notations. +Context {R : realType}. + +Lemma exec_normalize_return g x r : + projT1 (@execD _ g _ [Normalize return r:R]) x = \d_r :> probability _ R. +Proof. +by rewrite execD_normalize execP_return execD_real/= normalize_kdirac. +Qed. + +End trivial_example. + +Section sample_pair. +Local Open Scope lang_scope. +Local Open Scope ring_scope. +Import Notations. +Context {R : realType}. + +Definition sample_pair_syntax0 : @exp R _ [::] _ := + [let "x" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in + let "y" := Sample {exp_bernoulli (1 / 3%:R)%:nng (p1S 2)} in + return (#{"x"}, #{"y"})]. + +Definition sample_pair_syntax : exp _ [::] _ := + [Normalize {sample_pair_syntax0}]. + +Lemma exec_sample_pair0 (A : set (bool * bool)) : + @execP R [::] _ sample_pair_syntax0 tt A = + ((1 / 2)%:E * + ((1 / 3)%:E * ((true, true) \in A)%:R%:E + + (1 - 1 / 3)%:E * ((true, false) \in A)%:R%:E) + + (1 - 1 / 2)%:E * + ((1 / 3)%:E * ((false, true) \in A)%:R%:E + + (1 - 1 / 3)%:E * ((false, false) \in A)%:R%:E))%E. +Proof. +rewrite !execP_letin !execP_sample !execD_bernoulli execP_return /=. +rewrite execD_pair !exp_var'E (execD_var_erefl "x") (execD_var_erefl "y") /=. +rewrite letin'E integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !indicE !in_setT/= !mul1e. +rewrite !letin'E !integral_measure_add//= !ge0_integral_mscale//= /onem. +by rewrite !integral_dirac//= !indicE !in_setT/= !mul1e !diracE. +Qed. + +Lemma exec_sample_pair0_TandT : + @execP R [::] _ sample_pair_syntax0 tt [set (true, true)] = (1 / 6)%:E. +Proof. +rewrite exec_sample_pair0 mem_set//; do 3 rewrite memNset//=. +by rewrite /= !mule0 mule1 !add0e mule0 adde0; congr (_%:E); lra. +Qed. + +Lemma exec_sample_pair0_TandF : + @execP R [::] _ sample_pair_syntax0 tt [set (true, false)] = (1 / 3)%:E. +Proof. +rewrite exec_sample_pair0 memNset// mem_set//; do 2 rewrite memNset//. +by rewrite /= !mule0 mule1 !add0e mule0 adde0; congr (_%:E); lra. +Qed. + +Lemma exec_sample_pair0_TandT' : + @execP R [::] _ sample_pair_syntax0 tt [set p | p.1 && p.2] = (1 / 6)%:E. +Proof. +rewrite exec_sample_pair0 mem_set//; do 3 rewrite memNset//=. +by rewrite /= !mule0 mule1 !add0e mule0 adde0; congr (_%:E); lra. +Qed. + +Lemma exec_sample_pair_TorT : + (projT1 (execD sample_pair_syntax)) tt [set p | p.1 || p.2] = (2 / 3)%:E. +Proof. +rewrite execD_normalize normalizeE/= exec_sample_pair0. +do 4 rewrite mem_set//=. +rewrite eqe ifF; last by apply/negbTE/negP => /orP[/eqP|//]; lra. +rewrite exec_sample_pair0; do 3 rewrite mem_set//; rewrite memNset//=. +by rewrite !mule1; congr (_%:E); field. +Qed. + +End sample_pair. + +Section bernoulli_examples. +Local Open Scope ring_scope. +Local Open Scope lang_scope. +Import Notations. +Context {R : realType}. + +Definition bernoulli13_score := [Normalize + let "x" := Sample {@exp_bernoulli R [::] (1 / 3%:R)%:nng (p1S 2)} in + let "_" := if #{"x"} then Score {(1 / 3)}:R else Score {(2 / 3)}:R in + return #{"x"}]. + +Lemma exec_bernoulli13_score : + execD bernoulli13_score = execD (exp_bernoulli (1 / 5%:R)%:nng (p1S 4)). +Proof. +apply: eq_execD. +rewrite execD_bernoulli/= /bernoulli13_score execD_normalize 2!execP_letin. +rewrite execP_sample/= execD_bernoulli/= execP_if /= exp_var'E. +rewrite (execD_var_erefl "x")/= !execP_return/= 2!execP_score 2!execD_real/=. +apply: funext=> g; apply: eq_probability => U. +rewrite normalizeE !letin'E/=. +under eq_integral. + move=> x _. + rewrite !letin'E. + under eq_integral do rewrite retE /=. + over. +rewrite !integral_measure_add //=; last by move=> b _; rewrite integral_ge0. +rewrite !ge0_integral_mscale //=; last 2 first. + by move=> b _; rewrite integral_ge0. + by move=> b _; rewrite integral_ge0. +rewrite !integral_dirac// !indicE !in_setT !mul1e. +rewrite iteE/= !ge0_integral_mscale//=. +rewrite ger0_norm//. +rewrite !integral_indic//= !iteE/= /mscale/=. +rewrite setTI diracE !in_setT !mule1. +rewrite ger0_norm//. +rewrite -EFinD/= eqe ifF; last first. + by apply/negbTE/negP => /orP[/eqP|//]; rewrite /onem; lra. +rewrite !letin'E/= !iteE/=. +rewrite !ge0_integral_mscale//=. +rewrite ger0_norm//. +rewrite !integral_dirac//= !indicE !in_setT /= !mul1e ger0_norm//. +rewrite exp_var'E (execD_var_erefl "x")/=. +rewrite /bernoulli/= measure_addE/= /mscale/= !mul1r. +rewrite muleDl//; congr (_ + _)%E; + rewrite -!EFinM; + congr (_%:E); + by rewrite indicE /onem; case: (_ \in _); field. +Qed. + +Definition bernoulli12_score := [Normalize + let "x" := Sample {@exp_bernoulli R [::] (1 / 2)%:nng (p1S 1)} in + let "r" := if #{"x"} then Score {(1 / 3)}:R else Score {(2 / 3)}:R in + return #{"x"}]. + +Lemma exec_bernoulli12_score : + execD bernoulli12_score = execD (exp_bernoulli (1 / 3%:R)%:nng (p1S 2)). +Proof. +apply: eq_execD. +rewrite execD_bernoulli/= /bernoulli12_score execD_normalize 2!execP_letin. +rewrite execP_sample/= execD_bernoulli/= execP_if /= exp_var'E. +rewrite (execD_var_erefl "x")/= !execP_return/= 2!execP_score 2!execD_real/=. +apply: funext=> g; apply: eq_probability => U. +rewrite normalizeE !letin'E/=. +under eq_integral. + move=> x _. + rewrite !letin'E. + under eq_integral do rewrite retE /=. + over. +rewrite !integral_measure_add //=; last by move=> b _; rewrite integral_ge0. +rewrite !ge0_integral_mscale //=; last 2 first. + by move=> b _; rewrite integral_ge0. + by move=> b _; rewrite integral_ge0. +rewrite !integral_dirac// !indicE !in_setT !mul1e. +rewrite iteE/= !ge0_integral_mscale//=. +rewrite ger0_norm//. +rewrite !integral_indic//= !iteE/= /mscale/=. +rewrite setTI diracE !in_setT !mule1. +rewrite ger0_norm//. +rewrite -EFinD/= eqe ifF; last first. + apply/negbTE/negP => /orP[/eqP|//]. + by rewrite /onem; lra. +rewrite !letin'E/= !iteE/=. +rewrite !ge0_integral_mscale//=. +rewrite ger0_norm//. +rewrite !integral_dirac//= !indicE !in_setT /= !mul1e ger0_norm//. +rewrite exp_var'E (execD_var_erefl "x")/=. +rewrite /bernoulli/= measure_addE/= /mscale/= !mul1r. +rewrite muleDl//; congr (_ + _)%E; + rewrite -!EFinM; + congr (_%:E); + by rewrite indicE /onem; case: (_ \in _); field. +Qed. + +(* https://dl.acm.org/doi/pdf/10.1145/2933575.2935313 (Sect. 4) *) +Definition bernoulli14_score := [Normalize + let "x" := Sample {@exp_bernoulli R [::] (1 / 4%:R)%:nng (p1S 3)} in + let "r" := if #{"x"} then Score {5}:R else Score {2}:R in + return #{"x"}]. + +Let p511 : ((5%:R / 11%:R)%:nng%:num <= (1 : R)). +Proof. by rewrite /=; lra. Qed. + +Lemma exec_bernoulli14_score : + execD bernoulli14_score = execD (exp_bernoulli (5%:R / 11%:R)%:nng p511). +Proof. +apply: eq_execD. +rewrite execD_bernoulli/= execD_normalize 2!execP_letin. +rewrite execP_sample/= execD_bernoulli/= execP_if /= !exp_var'E. +rewrite !execP_return/= 2!execP_score 2!execD_real/=. +rewrite !(execD_var_erefl "x")/=. +apply: funext=> g; apply: eq_probability => U. +rewrite normalizeE !letin'E/=. +under eq_integral. + move=> x _. + rewrite !letin'E. + under eq_integral do rewrite retE /=. + over. +rewrite !integral_measure_add //=; last by move=> b _; rewrite integral_ge0. +rewrite !ge0_integral_mscale //=; last 2 first. + by move=> b _; rewrite integral_ge0. + by move=> b _; rewrite integral_ge0. +rewrite !integral_dirac// !indicE !in_setT !mul1e. +rewrite iteE/= !ge0_integral_mscale//=. +rewrite ger0_norm//. +rewrite !integral_indic//= !iteE/= /mscale/=. +rewrite setTI diracE !in_setT !mule1. +rewrite ger0_norm//. +rewrite -EFinD/= eqe ifF; last first. + apply/negbTE/negP => /orP[/eqP|//]. + by rewrite /onem; lra. +rewrite !letin'E/= !iteE/=. +rewrite !ge0_integral_mscale//=. +rewrite ger0_norm//. +rewrite !integral_dirac//= !indicE !in_setT /= !mul1e ger0_norm//. +rewrite /bernoulli/= measure_addE/= /mscale/= !mul1r. +rewrite muleDl//; congr (_ + _)%E; + rewrite -!EFinM; + congr (_%:E); + by rewrite indicE /onem; case: (_ \in _); field. +Qed. + +End bernoulli_examples. + +Section hard_constraint'. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). + +Definition fail' := + letin' (score (@measurable_cst _ _ X _ setT (0%R : R))) + (ret (@measurable_cst _ _ _ Y setT point)). + +Lemma fail'E x U : fail' x U = 0. +Proof. by rewrite /fail' letin'E ge0_integral_mscale//= normr0 mul0e. Qed. + +End hard_constraint'. +Arguments fail' {d d' X Y R}. + +(* hard constraints to express score below 1 *) +Lemma score_fail' d (X : measurableType d) {R : realType} + (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : + score (kr r%:num) = + letin' (sample_cst (bernoulli r1) : R.-pker X ~> _) + (ite macc0of2 (ret ktt) fail'). +Proof. +apply/eq_sfkernel => x U. +rewrite letin'E/= /sample; unlock. +rewrite integral_measure_add//= ge0_integral_mscale//= ge0_integral_mscale//=. +rewrite integral_dirac//= integral_dirac//= !indicT/= !mul1e. +by rewrite /mscale/= iteE//= iteE//= fail'E mule0 adde0 ger0_norm. +Qed. + +Section hard_constraint. +Local Open Scope ring_scope. +Local Open Scope lang_scope. +Import Notations. +Context {R : realType} {str : string}. + +Definition hard_constraint g : @exp R _ g _ := + [let str := Score {0}:R in return TT]. + +Lemma exec_hard_constraint g mg U : + execP (hard_constraint g) mg U = fail' (false, tt) U. +Proof. +rewrite execP_letin execP_score execD_real execP_return execD_unit/=. +rewrite letin'E integral_indic//= /mscale/= normr0 mul0e. +by rewrite /fail' letin'E/= ge0_integral_mscale//= normr0 mul0e. +Qed. + +Lemma exec_score_fail (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : + execP (g := [::]) [Score {r%:num}:R] = + execP [let str := Sample {exp_bernoulli r r1} in + if #str then return TT else {hard_constraint _}]. +Proof. +rewrite execP_score execD_real /= score_fail'. +rewrite execP_letin execP_sample/= execD_bernoulli execP_if execP_return. +rewrite execD_unit/= exp_var'E /=. + apply/ctx_prf_head. +move=> h. +apply: eq_sfkernel=> /= -[] U. +rewrite 2!letin'E/=. +apply: eq_integral => b _. +rewrite 2!iteE//=. +case: b => //=. +- suff : projT1 (@execD R _ _ (exp_var str h)) (true, tt) = true by move=> ->. + set g := [:: (str, Bool)]. + have /= := @execD_var R [:: (str, Bool)] str. + by rewrite eqxx => /(_ h) ->. +- have -> : projT1 (@execD R _ _ (exp_var str h)) (false, tt) = false. + set g := [:: (str, Bool)]. + have /= := @execD_var R [:: (str, Bool)] str. + by rewrite eqxx /= => /(_ h) ->. + by rewrite (@exec_hard_constraint [:: (str, Bool)]). +Qed. + +End hard_constraint. + +Section letinA. +Local Open Scope lang_scope. +Variable R : realType. + +Lemma letinA g x y t1 t2 t3 (xyg : x \notin dom ((y, t2) :: g)) + (e1 : @exp R P g t1) + (e2 : exp P [:: (x, t1) & g] t2) + (e3 : exp P [:: (y, t2) & g] t3) : + forall U, measurable U -> + execP [let x := e1 in + let y := e2 in + {@exp_weak _ _ [:: (y, t2)] _ _ (x, t1) e3 xyg}] ^~ U = + execP [let y := + let x := e1 in e2 in + e3] ^~ U. +Proof. +move=> U mU; apply/funext=> z1. +rewrite !execP_letin. +rewrite (execP_weak [:: (y, t2)]). +apply: letin'A => //= z2 z3. +rewrite /kweak /mctx_strong /=. +by destruct z3. +Qed. + +Example letinA12 : forall U, measurable U -> + @execP R [::] _ [let "y" := return {1}:R in + let "x" := return {2}:R in + return #{"x"}] ^~ U = + @execP R [::] _ [let "x" := + let "y" := return {1}:R in return {2}:R in + return #{"x"}] ^~ U. +Proof. +move=> U mU. +rewrite !execP_letin !execP_return !execD_real. +apply: funext=> x. +rewrite !exp_var'E /= !(execD_var_erefl "x")/=. +exact: letin'A. +Qed. + +End letinA. + +Section staton_bus. +Local Open Scope ring_scope. +Local Open Scope lang_scope. +Import Notations. +Context {R : realType}. + +Definition staton_bus_syntax0 : @exp R _ [::] _ := + [let "x" := Sample {exp_bernoulli (2 / 7%:R)%:nng p27} in + let "r" := if #{"x"} then return {3}:R else return {10}:R in + let "_" := Score {exp_poisson 4 [#{"r"}]} in + return #{"x"}]. + +Definition staton_bus_syntax := [Normalize {staton_bus_syntax0}]. + +Let sample_bern : R.-sfker munit ~> mbool := sample_cst (bernoulli p27). + +Let ite_3_10 : R.-sfker mbool * munit ~> (mR R) := + ite macc0of2 (ret k3) (ret k10). + +Let score_poisson4 : R.-sfker mR R * (mbool * munit) ~> munit := + score (measurableT_comp (measurable_poisson 4) macc0of2). + +Let kstaton_bus' := + letin' sample_bern + (letin' ite_3_10 + (letin' score_poisson4 (ret macc2of4'))). + +Lemma eval_staton_bus0 : staton_bus_syntax0 -P> kstaton_bus'. +Proof. +apply: eval_letin; first by apply: eval_sample; exact: eval_bernoulli. +apply: eval_letin. + apply/evalP_if; [|exact/eval_return/eval_real..]. + rewrite exp_var'E. + by apply/execD_evalD; rewrite (execD_var_erefl "x")/=; congr existT. +apply: eval_letin. + apply/eval_score/eval_poisson. + rewrite exp_var'E. + by apply/execD_evalD; rewrite (execD_var_erefl "r")/=; congr existT. +apply/eval_return/execD_evalD. +by rewrite exp_var'E (execD_var_erefl "x")/=; congr existT. +Qed. + +Lemma exec_staton_bus0' : execP staton_bus_syntax0 = kstaton_bus'. +Proof. +rewrite 3!execP_letin execP_sample/= execD_bernoulli. +rewrite /kstaton_bus'; congr letin'. +rewrite !execP_if !execP_return !execD_real/=. +rewrite exp_var'E (execD_var_erefl "x")/=. +have -> : measurable_acc_typ [:: Bool] 0 = macc0of2 by []. +congr letin'. +rewrite execP_score execD_poisson/=. +rewrite exp_var'E (execD_var_erefl "r")/=. +have -> : measurable_acc_typ [:: Real; Bool] 0 = macc0of2 by []. +congr letin'. +by rewrite exp_var'E (execD_var_erefl "x") /=; congr ret. +Qed. + +Lemma exec_staton_bus : execD staton_bus_syntax = + existT _ (normalize kstaton_bus' point) (measurable_fun_mnormalize _). +Proof. by rewrite execD_normalize exec_staton_bus0'. Qed. + +Let poisson4 := @poisson R 4%N. + +Let staton_bus_probability U := + ((2 / 7)%:E * (poisson4 3)%:E * \d_true U + + (5 / 7)%:E * (poisson4 10)%:E * \d_false U)%E. + +Lemma exec_staton_bus0 (U : set bool) : + execP staton_bus_syntax0 tt U = staton_bus_probability U. +Proof. +rewrite exec_staton_bus0' /staton_bus_probability /kstaton_bus'. +rewrite letin'_sample_bernoulli. +rewrite -!muleA; congr (_ * _ + _ * _)%E. +- rewrite letin'_iteT//. + rewrite letin'_retk//. + rewrite letin'_kret//. + rewrite /score_poisson4. + by rewrite /score/= /mscale/= ger0_norm//= poisson_ge0. +- by rewrite onem27. +- rewrite letin'_iteF//. + rewrite letin'_retk//. + rewrite letin'_kret//. + rewrite /score_poisson4. + by rewrite /score/= /mscale/= ger0_norm//= poisson_ge0. +Qed. + +End staton_bus. + +(* same as staton_bus module associativity of letin *) +Section staton_busA. +Local Open Scope ring_scope. +Local Open Scope lang_scope. +Import Notations. +Context {R : realType}. + +Definition staton_busA_syntax0 : @exp R _ [::] _ := + [let "x" := Sample {exp_bernoulli (2 / 7%:R)%:nng p27} in + let "_" := + let "r" := if #{"x"} then return {3}:R else return {10}:R in + Score {exp_poisson 4 [#{"r"}]} in + return #{"x"}]. + +Definition staton_busA_syntax : exp _ [::] _ := + [Normalize {staton_busA_syntax0}]. + +Let sample_bern : R.-sfker munit ~> mbool := sample_cst (bernoulli p27). + +Let ite_3_10 : R.-sfker mbool * munit ~> (mR R) := + ite macc0of2 (ret k3) (ret k10). + +Let score_poisson4 : R.-sfker mR R * (mbool * munit) ~> munit := + score (measurableT_comp (measurable_poisson 4) macc0of3'). + +(* same as kstaton_bus _ (measurable_poisson 4) but expressed with letin' + instead of letin *) +Let kstaton_busA' := + letin' sample_bern + (letin' + (letin' ite_3_10 + score_poisson4) + (ret macc1of3')). + +(*Lemma kstaton_busA'E : kstaton_busA' = kstaton_bus _ (measurable_poisson 4). +Proof. +apply/eq_sfkernel => -[] U. +rewrite /kstaton_busA' /kstaton_bus. +rewrite letin'_letin. +rewrite /sample_bern. +congr (letin _ _ tt U). +rewrite 2!letin'_letin/=. +Abort.*) + +Lemma eval_staton_busA0 : staton_busA_syntax0 -P> kstaton_busA'. +Proof. +apply: eval_letin; first by apply: eval_sample; exact: eval_bernoulli. +apply: eval_letin. + apply: eval_letin. + apply/evalP_if; [|exact/eval_return/eval_real..]. + rewrite exp_var'E. + by apply/execD_evalD; rewrite (execD_var_erefl "x")/=; congr existT. + apply/eval_score/eval_poisson. + rewrite exp_var'E. + by apply/execD_evalD; rewrite (execD_var_erefl "r")/=; congr existT. +apply/eval_return. +by apply/execD_evalD; rewrite exp_var'E (execD_var_erefl "x")/=; congr existT. +Qed. + +Lemma exec_staton_busA0' : execP staton_busA_syntax0 = kstaton_busA'. +Proof. +rewrite 3!execP_letin execP_sample/= execD_bernoulli. +rewrite /kstaton_busA'; congr letin'. +rewrite !execP_if !execP_return !execD_real/=. +rewrite exp_var'E (execD_var_erefl "x")/=. +have -> : measurable_acc_typ [:: Bool] 0 = macc0of2 by []. +congr letin'. + rewrite execP_score execD_poisson/=. + rewrite exp_var'E (execD_var_erefl "r")/=. + by have -> : measurable_acc_typ [:: Real; Bool] 0 = macc0of3' by []. +by rewrite exp_var'E (execD_var_erefl "x") /=; congr ret. +Qed. + +Lemma exec_statonA_bus : execD staton_busA_syntax = + existT _ (normalize kstaton_busA' point) (measurable_fun_mnormalize _). +Proof. by rewrite execD_normalize exec_staton_busA0'. Qed. + +(* equivalence between staton_bus and staton_busA *) +Lemma staton_bus_staton_busA : + execP staton_bus_syntax0 = @execP R _ _ staton_busA_syntax0. +Proof. +rewrite /staton_bus_syntax0 /staton_busA_syntax0. +rewrite execP_letin. +rewrite [in RHS]execP_letin. +congr (letin' _). +set e1 := exp_if _ _ _. +set e2 := exp_score _. +set e3 := (exp_return _ in RHS). +pose f := @found _ Unit "x" Bool [::]. +have r_f : "r" \notin [seq i.1 | i <- ("_", Unit) :: untag (ctx_of f)] by []. +have H := @letinA _ _ _ _ _ _ + (lookup Unit (("_", Unit) :: untag (ctx_of f)) "x") + r_f e1 e2 e3. +apply/eq_sfkernel => /= x U. +have mU : + (@mtyp_disp R (lookup Unit (("_", Unit) :: untag (ctx_of f)) "x")).-measurable U. + by []. +move: H => /(_ U mU) /(congr1 (fun f => f x)) <-. +set e3' := exp_return _. +set e3_weak := exp_weak _ _ _ _. +rewrite !execP_letin. +suff: execP e3' = execP (e3_weak e3 r_f) by move=> <-. +rewrite execP_return/= exp_var'E (execD_var_erefl "x") /= /e3_weak. +rewrite (@execP_weak R [:: ("_", Unit)] _ ("r", Real) _ e3 r_f). +rewrite execP_return exp_var'E/= (execD_var_erefl "x") //=. +by apply/eq_sfkernel => /= -[[] [a [b []]]] U0. +Qed. + +Let poisson4 := @poisson R 4%N. + +Lemma exec_staton_busA0 U : execP staton_busA_syntax0 tt U = + ((2 / 7%:R)%:E * (poisson4 3%:R)%:E * \d_true U + + (5%:R / 7%:R)%:E * (poisson4 10%:R)%:E * \d_false U)%E. +Proof. by rewrite -staton_bus_staton_busA exec_staton_bus0. Qed. + +End staton_busA. + +Section letinC. +Local Open Scope lang_scope. +Variable (R : realType). + +Require Import Classical_Prop. + +Lemma letinC g t1 t2 (e1 : @exp R P g t1) (e2 : exp P g t2) + (x y : string) + (xy : infer (x != y)) (yx : infer (y != x)) + (xg : x \notin dom g) (yg : y \notin dom g) : + forall U, measurable U -> + execP [ + let x := e1 in + let y := {exp_weak _ [::] _ (x, t1) e2 xg} in + return (#x, #y)] ^~ U = + execP [ + let y := e2 in + let x := {exp_weak _ [::] _ (y, t2) e1 yg} in + return (#x, #y)] ^~ U. +Proof. +move=> U mU; apply/funext => z. +rewrite 4!execP_letin. +rewrite 2!(execP_weak [::] g). +rewrite 2!execP_return/=. +rewrite 2!execD_pair/=. +rewrite !exp_var'E. +- exact/(ctx_prf_tail _ yx)/ctx_prf_head. +- exact/ctx_prf_head. +- exact/ctx_prf_head. +- exact/(ctx_prf_tail _ xy)/ctx_prf_head. +- move=> h1 h2 h3 h4. + set g1 := [:: (y, t2), (x, t1) & g]. + set g2 := [:: (x, t1), (y, t2) & g]. + have /= := @execD_var R g1 x. + rewrite (negbTE yx) eqxx => /(_ h4) ->. + have /= := @execD_var R g2 x. + rewrite (negbTE yx) eqxx => /(_ h2) ->. + have /= := @execD_var R g1 y. + rewrite eqxx => /(_ h3) ->. + have /= := @execD_var R g2 y. + rewrite (negbTE xy) eqxx => /(_ h1) -> /=. + have -> : measurable_acc_typ [:: t2, t1 & map snd g] 0 = macc0of3' by []. + have -> : measurable_acc_typ [:: t2, t1 & map snd g] 1 = macc1of3' by []. + rewrite (letin'C _ _ (execP e2) + [the R.-sfker _ ~> _ of @kweak _ [::] _ (y, t2) _ (execP e1)]); + [ |by [] | by [] |by []]. + have -> : measurable_acc_typ [:: t1, t2 & map snd g] 0 = macc0of3' by []. + by have -> : measurable_acc_typ [:: t1, t2 & map snd g] 1 = macc1of3' by []. +Qed. + +Example letinC_ground_variables g t1 t2 (e1 : @exp R P g t1) (e2 : exp P g t2) + (x := "x") (y := "y") + (xg : x \notin dom g) (yg : y \notin dom g) : + forall U, measurable U -> + execP [ + let x := e1 in + let y := {exp_weak _ [::] _ (x, t1) e2 xg} in + return (#x, #y)] ^~ U = + execP [ + let y := e2 in + let x := {exp_weak _ [::] _ (y, t2) e1 yg} in + return (#x, #y)] ^~ U. +Proof. by move=> U mU; rewrite letinC. Qed. + +Example letinC_ground (g := [:: ("a", Unit); ("b", Bool)]) t1 t2 + (e1 : @exp R P g t1) + (e2 : exp P g t2) : + forall U, measurable U -> + execP [let "x" := e1 in + let "y" := e2 :+ {"x"} in + return (#{"x"}, #{"y"})] ^~ U = + execP [let "y" := e2 in + let "x" := e1 :+ {"y"} in + return (#{"x"}, #{"y"})] ^~ U. +Proof. move=> U mU; exact: letinC. Qed. + +End letinC. diff --git a/theories/lang_syntax_toy.v b/theories/lang_syntax_toy.v new file mode 100644 index 000000000..a328ea1cd --- /dev/null +++ b/theories/lang_syntax_toy.v @@ -0,0 +1,550 @@ +Require Import String Classical. +From HB Require Import structures. +From mathcomp Require Import all_ssreflect ssralg. +From mathcomp.classical Require Import mathcomp_extra boolp. +Require Import signed reals topology normedtype. +Require Import lang_syntax_util. + +(******************************************************************************) +(* Intrinsically-typed concrete syntax for a toy language *) +(* *) +(* The main module provided by this file is "lang_intrinsic_tysc" which *) +(* provides an example of intrinsically-typed concrete syntax for a toy *) +(* language (a simplification of the syntax/evaluation formalized in *) +(* lang_syntax.v). Other modules provide even more simplified language for *) +(* pedagogical purposes. *) +(* *) +(* lang_extrinsic == non-intrinsic definition of expression *) +(* lang_intrinsic_ty == intrinsically-typed syntax *) +(* lang_intrinsic_sc == intrinsically-scoped syntax *) +(* lang_intrinsic_tysc == intrinsically-typed/scoped syntax *) +(* *) +(******************************************************************************) + +Set Implicit Arguments. +Unset Strict Implicit. +Set Printing Implicit Defensive. + +Import numFieldTopology.Exports. + +Local Open Scope classical_set_scope. +Local Open Scope ring_scope. +Local Open Scope ereal_scope. + +Section type. +Variables (R : realType). + +Inductive typ := Real | Unit. + +Canonical typ_eqType := Equality.Pack (@gen_eqMixin typ). + +Definition iter_pair (l : list Type) : Type := + foldr (fun x y => (x * y)%type) unit l. + +Definition Type_of_typ (t : typ) : Type := + match t with + | Real => R + | Unit => unit + end. + +Definition ctx := seq (string * typ). + +Definition Type_of_ctx (g : ctx) := iter_pair (map (Type_of_typ \o snd) g). + +Goal Type_of_ctx [:: ("x", Real); ("y", Real)] = (R * (R * unit))%type. +Proof. by []. Qed. + +End type. + +Module lang_extrinsic. +Section lang_extrinsic. +Variable R : realType. +Implicit Types str : string. + +Inductive exp : Type := +| exp_unit : exp +| exp_real : R -> exp +| exp_var (g : ctx) t str : t = lookup Unit g str -> exp +| exp_add : exp -> exp -> exp +| exp_letin str : exp -> exp -> exp. +Arguments exp_var {g t}. + +Fail Example letin_once : exp := + exp_letin "x" (exp_real 1) (exp_var "x" erefl). +Example letin_once : exp := + exp_letin "x" (exp_real 1) (@exp_var [:: ("x", Real)] Real "x" erefl). + +End lang_extrinsic. +End lang_extrinsic. + +Module lang_intrinsic_ty. +Section lang_intrinsic_ty. +Variable R : realType. +Implicit Types str : string. + +Inductive exp : typ -> Type := +| exp_unit : exp Unit +| exp_real : R -> exp Real +| exp_var g t str : t = lookup Unit g str -> exp t +| exp_add : exp Real -> exp Real -> exp Real +| exp_letin t u : string -> exp t -> exp u -> exp u. +Arguments exp_var {g t}. + +Fail Example letin_once : exp Real := + exp_letin "x" (exp_real 1) (exp_var "x" erefl). +Example letin_once : exp Real := + exp_letin "x" (exp_real 1) (@exp_var [:: ("x", Real)] _ "x" erefl). + +End lang_intrinsic_ty. +End lang_intrinsic_ty. + +Module lang_intrinsic_sc. +Section lang_intrinsic_sc. +Variable R : realType. +Implicit Types str : string. + +Inductive exp : ctx -> Type := +| exp_unit g : exp g +| exp_real g : R -> exp g +| exp_var g t str : t = lookup Unit g str -> exp g +| exp_add g : exp g -> exp g -> exp g +| exp_letin g t str : exp g -> exp ((str, t) :: g) -> exp g. +Arguments exp_real {g}. +Arguments exp_var {g t}. +Arguments exp_letin {g t}. + +Declare Custom Entry expr. + +Notation "[ e ]" := e (e custom expr at level 5). +Notation "{ x }" := x (in custom expr, x constr). +Notation "x ':R'" := (exp_real x) (in custom expr at level 1). +Notation "x" := x (in custom expr at level 0, x ident). +Notation "$ x" := (exp_var x erefl) (in custom expr at level 1). +Notation "x + y" := (exp_add x y) + (in custom expr at level 2, left associativity). +Notation "'let' x ':=' e1 'in' e2" := (exp_letin x e1 e2) + (in custom expr at level 3, x constr, + e1 custom expr at level 2, e2 custom expr at level 3, + left associativity). + +Fail Example letin_once : exp [::] := + [let "x" := {1%R}:R in ${"x"}]. +Example letin_once : exp [::] := + [let "x" := {1%R}:R in {@exp_var [:: ("x", Real)] _ "x" erefl}]. + +Fixpoint acc (g : ctx) (i : nat) : + Type_of_ctx R g -> @Type_of_typ R (nth Unit (map snd g) i) := + match g return Type_of_ctx R g -> Type_of_typ R (nth Unit (map snd g) i) with + | [::] => match i with | O => id | j.+1 => id end + | _ :: _ => match i with + | O => fst + | j.+1 => fun H => acc j H.2 + end + end. +Arguments acc : clear implicits. + +Inductive eval : forall g (t : typ), exp g -> (Type_of_ctx R g -> Type_of_typ R t) -> Prop := +| eval_real g c : @eval g Real [c:R] (fun=> c) +| eval_plus g (e1 e2 : exp g) (v1 v2 : R) : + @eval g Real e1 (fun=> v1) -> + @eval g Real e2 (fun=> v2) -> + @eval g Real [e1 + e2] (fun=> v1 + v2) +| eval_var (g : ctx) str i : + i = index str (map fst g) -> eval [$ str] (acc g i). + +Goal @eval [::] Real [{1}:R] (fun=> 1). +Proof. exact: eval_real. Qed. +Goal @eval [::] Real [{1}:R + {2}:R] (fun=> 3). +Proof. exact/eval_plus/eval_real/eval_real. Qed. +Goal @eval [:: ("x", Real)] _ [$ {"x"}] (acc [:: ("x", Real)] 0). +Proof. exact: eval_var. Qed. + +End lang_intrinsic_sc. +End lang_intrinsic_sc. + +Module lang_intrinsic_tysc. +Section lang_intrinsic_tysc. +Variable R : realType. +Implicit Types str : string. + +Inductive typ := Real | Unit | Pair : typ -> typ -> typ. + +Canonical typ_eqType := Equality.Pack (@gen_eqMixin typ). + +Fixpoint mtyp (t : typ) : Type := + match t with + | Real => R + | Unit => unit + | Pair t1 t2 => (mtyp t1 * mtyp t2) + end. + +Definition ctx := seq (string * typ). + +Definition Type_of_ctx (g : ctx) := iter_pair (map (mtyp \o snd) g). + +Goal Type_of_ctx [:: ("x", Real); ("y", Real)] = (R * (R * unit))%type. +Proof. by []. Qed. + +Inductive exp : ctx -> typ -> Type := +| exp_unit g : exp g Unit +| exp_real g : R -> exp g Real +| exp_var g t str : t = lookup Unit g str -> exp g t +| exp_add g : exp g Real -> exp g Real -> exp g Real +| exp_pair g t1 t2 : exp g t1 -> exp g t2 -> exp g (Pair t1 t2) +| exp_letin g t1 t2 x : exp g t1 -> exp ((x, t1) :: g) t2 -> exp g t2. + +Definition exp_var' str (t : typ) (g : find str t) := + @exp_var (untag (ctx_of g)) t str (ctx_prf g). + +Section no_bidirectional_hints. + +Arguments exp_unit {g}. +Arguments exp_real {g}. +Arguments exp_var {g t}. +Arguments exp_add {g}. +Arguments exp_pair {g t1 t2}. +Arguments exp_letin {g t1 t2}. +Arguments exp_var' str {t} g. + +Fail Example letin_add : exp [::] _ := + exp_letin "x" (exp_real 1) + (exp_letin "y" (exp_real 2) + (exp_add (exp_var "x" erefl) + (exp_var "y" erefl))). +Example letin_add : exp [::] _ := + exp_letin "x" (exp_real 1) + (exp_letin "y" (exp_real 2) + (exp_add (@exp_var [:: ("y", Real); ("x", Real)] _ "x" erefl) + (exp_var "y" erefl))). +Reset letin_add. + +Declare Custom Entry expr. + +Notation "[ e ]" := e (e custom expr at level 5). +Notation "{ x }" := x (in custom expr, x constr). +Notation "x ':R'" := (exp_real x) (in custom expr at level 1). +Notation "x" := x (in custom expr at level 0, x ident). +Notation "$ x" := (exp_var x erefl) (in custom expr at level 1). +Notation "# x" := (exp_var' x%string _) (in custom expr at level 1). +Notation "e1 + e2" := (exp_add e1 e2) + (in custom expr at level 2, + (* e1 custom expr at level 1, e2 custom expr at level 2, *) + left associativity). +Notation "( e1 , e2 )" := (exp_pair e1 e2) + (in custom expr at level 1). +Notation "'let' x ':=' e1 'in' e2" := (exp_letin x e1 e2) + (in custom expr at level 3, x constr, + e1 custom expr at level 2, e2 custom expr at level 3, + left associativity). + +Fail Definition let3_add_erefl (a b c : string) + (ba : infer (b != a)) (ca : infer (c != a)) (cb : infer (c != b)) + (ab : infer (a != b)) (ac : infer (a != c)) (bc : infer (b != c)) + : exp [::] _ := [ + let a := {1}:R in + let b := {2}:R in + let c := {3}:R in + $a + $b]. +(* The term "[$ a]" has type "exp ?g2 (lookup Unit ?g2 a)" while it is expected to have type "exp ?g2 Real". *) + +Definition let3_pair_erefl (a b c : string) + (ba : infer (b != a)) (ca : infer (c != a)) (cb : infer (c != b)) + (ab : infer (a != b)) (ac : infer (a != c)) (bc : infer (b != c)) + : exp [::] _ := [ + let a := {1}:R in + let b := {2}:R in + let c := {3}:R in + ($a, $b)]. + +Fail Definition let3_add (a b c : string) + (ba : infer (b != a)) (ca : infer (c != a)) (cb : infer (c != b)) + (ab : infer (a != b)) (ac : infer (a != c)) (bc : infer (b != c)) + : exp [::] _ := [ + let a := {1}:R in + let b := {2}:R in + let c := {3}:R in + #a + #b]. +(* The term "[# a + # b]" has type + "exp (untag (ctx_of (recurse (str':=b) Real ?f))) Real" +while it is expected to have type "exp ((c, Real) :: ?g1) ?u1" +(cannot unify "(b, Real)" and "(c, Real)"). *) + +Fail Definition let3_pair (a b c : string) + (ba : infer (b != a)) (ca : infer (c != a)) (cb : infer (c != b)) + (ab : infer (a != b)) (ac : infer (a != c)) (bc : infer (b != c)) + : exp [::] _ := [ + let a := {1}:R in + let b := {2}:R in + let c := {3}:R in + (#a, #b)]. +(* The term "[# a + # b]" has type "exp (untag (ctx_of (recurse (str':=b) Real ?f))) Real" while it is expected to have type + "exp ((c, Real) :: ?g1) ?u1" (cannot unify "(b, Real)" and "(c, Real)"). *) + +End no_bidirectional_hints. + +Section with_bidirectional_hints. + +Arguments exp_unit {g}. +Arguments exp_real {g}. +Arguments exp_var {g t}. +Arguments exp_add {g} &. +Arguments exp_pair {g} & {t1 t2}. +Arguments exp_letin {g} & {t1 t2}. +Arguments exp_var' str {t} g. + +Declare Custom Entry expr. + +Notation "[ e ]" := e (e custom expr at level 5). +Notation "{ x }" := x (in custom expr, x constr). +Notation "x ':R'" := (exp_real x) (in custom expr at level 1). +Notation "x" := x (in custom expr at level 0, x ident). +Notation "$ x" := (exp_var x%string erefl) (in custom expr at level 1). +Notation "# x" := (exp_var' x%string _) (in custom expr at level 1). +Notation "e1 + e2" := (exp_add e1 e2) + (in custom expr at level 2, + left associativity). +Notation "( e1 , e2 )" := (exp_pair e1 e2) + (in custom expr at level 1). +Notation "'let' x ':=' e1 'in' e2" := (exp_letin x e1 e2) + (in custom expr at level 3, x constr, + e1 custom expr at level 2, e2 custom expr at level 3, + left associativity). + +Fail Definition let2_add_erefl_bidi (a b : string) + (ba : infer (b != a)) (ab : infer (a != b)) + : exp [::] _ := [ + let a := {1}:R in + let b := {2}:R in + $a + $b]. + +Definition let2_add_erefl_bidi (a b : string) + (ba : infer (b != a)) (ab : infer (a != b)) + : exp [::] _ := [ + let a := {1}:R in + let b := {2}:R in + #a + #b]. + +Fail Definition let3_add_erefl_bidi (a b c d : string) + (ba : infer (b != a)) (ca : infer (c != a)) (cb : infer (c != b)) + (ab : infer (a != b)) (ac : infer (a != c)) (bc : infer (b != c)) + : exp [::] _ := [ + let a := {1}:R in + let b := {2}:R in + let c := {3}:R in + $a + $b]. +(* The term "[$ a]" has type "exp [:: (c, Real); (b, Real); (a, Real)] (lookup Unit [:: (c, Real); (b, Real); (a, Real)] a)" +while it is expected to have type "exp [:: (c, Real); (b, Real); (a, Real)] Real" +(cannot unify "lookup Unit [:: (c, Real); (b, Real); (a, Real)] a" and "Real"). *) + +Definition let3_pair_erefl_bidi (a b c : string) + (ba : infer (b != a)) (ca : infer (c != a)) (cb : infer (c != b)) + (ab : infer (a != b)) (ac : infer (a != c)) (bc : infer (b != c)) + : exp [::] _ := [ + let a := {1}:R in + let b := {2}:R in + let c := {3}:R in + ($a, $b)]. + +Check let3_pair_erefl_bidi. +(* exp [::] (Pair (lookup Unit [:: (c, Real); (b, Real); (a, Real)] a) (lookup Unit [:: (c, Real); (b, Real); (a, Real)] b)) *) + +Definition let3_add_bidi (a b c : string) + (ba : infer (b != a)) (ca : infer (c != a)) (cb : infer (c != b)) + (ab : infer (a != b)) (ac : infer (a != c)) (bc : infer (b != c)) + : exp [::] _ := [ + let a := {1}:R in + let b := {2}:R in + let c := {3}:R in + #a + #b]. + +Definition let3_pair_bidi (a b c : string) + (ba : infer (b != a)) (ca : infer (c != a)) (cb : infer (c != b)) + (ab : infer (a != b)) (ac : infer (a != c)) (bc : infer (b != c)) + : exp [::] _ := [ + let a := {1}:R in + let b := {2}:R in + let c := {3}:R in + (#a , #b)]. + +Check let3_pair_bidi. +(* exp [::] (Pair Real Real) *) + +Example e0 : exp [::] _ := exp_real 1. +Example letin1 : exp [::] _ := + exp_letin "x" (exp_real 1) (exp_var "x" erefl). +Example letin2 : exp [::] _ := + exp_letin "x" (exp_real 1) + (exp_letin "y" (exp_real 2) + (exp_var "x" erefl)). + +Example letin_add : exp [::] _ := + exp_letin "x" (exp_real 1) + (exp_letin "y" (exp_real 2) + (exp_add (exp_var "x" erefl) + (exp_var "y" erefl))). +Reset letin_add. +Fail Example letin_add (x y : string) + (xy : infer (x != y)) (yx : infer (y != x)) : exp [::] _ := + exp_letin x (exp_real 1) + (exp_letin y (exp_real 2) + (exp_add (exp_var x erefl) (exp_var y erefl))). +Example letin_add (x y : string) + (xy : infer (x != y)) (yx : infer (y != x)) : exp [::] _ := + exp_letin x (exp_real 1) + (exp_letin y (exp_real 2) + (exp_add (exp_var' x _) (exp_var' y _))). +Reset letin_add. + +Example letin_add_custom : exp [::] _ := + [let "x" := {1}:R in + let "y" := {2}:R in + #{"x"} + #{"y"}]. + +Section eval. + +Fixpoint acc (g : ctx) (i : nat) : + Type_of_ctx g -> mtyp (nth Unit (map snd g) i) := + match g return Type_of_ctx g -> mtyp (nth Unit (map snd g) i) with + | [::] => match i with | O => id | j.+1 => id end + | _ :: _ => match i with + | O => fst + | j.+1 => fun H => acc j H.2 + end + end. +Arguments acc : clear implicits. + +Reserved Notation "e '-e->' v" (at level 40). + +Inductive eval : forall g t, exp g t -> (Type_of_ctx g -> mtyp t) -> Prop := +| eval_tt g : (exp_unit : exp g _) -e-> (fun=> tt) +| eval_real g c : (exp_real c : exp g _) -e-> (fun=> c) +| eval_plus g (e1 e2 : exp g Real) v1 v2 : + e1 -e-> v1 -> + e2 -e-> v2 -> + [e1 + e2] -e-> fun x => v1 x + v2 x +| eval_var g str : + let i := index str (map fst g) in + exp_var str erefl -e-> acc g i +| eval_pair g t1 t2 e1 e2 v1 v2 : + e1 -e-> v1 -> + e2 -e-> v2 -> + @exp_pair g t1 t2 e1 e2 -e-> fun x => (v1 x, v2 x) +| eval_letin g t t' str (e1 : exp g t) (e2 : exp ((str, t) :: g) t') v1 v2 : + e1 -e-> v1 -> + e2 -e-> v2 -> + exp_letin str e1 e2 -e-> (fun a => v2 (v1 a, a)) +where "e '-e->' v" := (@eval _ _ e v). + +Lemma eval_uniq g t (e : exp g t) u v : + e -e-> u -> e -e-> v -> u = v. +Proof. +move=> hu. +apply: (@eval_ind + (fun g t (e : exp g t) (u : Type_of_ctx g -> mtyp t) => + forall v, e -e-> v -> u = v)); last exact: hu. +all: (rewrite {g t e u v hu}). +- move=> g v. + inversion 1. + by inj_ex H3. +- move=> g c v. + inversion 1. + by inj_ex H3. +- move=> g e1 e2 v1 v2 ev1 IH1 ev2 IH2 v. + inversion 1. + inj_ex H0; inj_ex H1; subst. + inj_ex H5; subst. + by rewrite (IH1 _ H3) (IH2 _ H4). +- move=> g x i v. + inversion 1. + by inj_ex H6; subst. +- move=> g t1 t2 e1 e2 v1 v2 ev1 IH1 ev2 IH2 v. + inversion 1. + inj_ex H3; inj_ex H4; subst. + inj_ex H5; subst. + by rewrite (IH1 _ H6) (IH2 _ H7). +- move=> g t t' x0 e0 e1 v1 v2 ev1 IH1 ev2 IH2 v. + inversion 1. + inj_ex H5; subst. + inj_ex H6; subst. + inj_ex H7; subst. + by rewrite (IH1 _ H4) (IH2 _ H8). +Qed. + +Lemma eval_total g t (e : exp g t) : exists v, e -e-> v. +Proof. +elim: e. +- by eexists; exact: eval_tt. +- by eexists; exact: eval_real. +- move=> {}g {}t x e; subst t. + by eexists; exact: eval_var. +- move=> {}g e1 [v1] IH1 e2 [v2] IH2. + by eexists; exact: (eval_plus IH1 IH2). +- move=> {}g t1 t2 e1 [v1] IH1 e2 [v2] IH2. + by eexists; exact: (eval_pair IH1 IH2). +- move=> {}g {}t u x e1 [v1] IH1 e2 [v2] IH2. + by eexists; exact: (eval_letin IH1 IH2). +Qed. + +Definition exec g t (e : exp g t) : Type_of_ctx g -> mtyp t := + proj1_sig (cid (@eval_total g t e)). + +Lemma exec_eval g t (e : exp g t) v : exec e = v <-> e -e-> v. +Proof. +split. + by move=> <-; rewrite /exec; case: cid. +move=> ev. +by rewrite /exec; case: cid => f H/=; apply: eval_uniq; eauto. +Qed. + +Lemma eval_exec g t (e : exp g t) : e -e-> exec e. +Proof. by rewrite /exec; case: cid. Qed. + +Lemma exec_real g r : @exec g Real (exp_real r) = (fun=> r). +Proof. exact/exec_eval/eval_real. Qed. + +Lemma exec_var g str t H : + exec (@exp_var _ t str H) = + eq_rect _ (fun a => Type_of_ctx g -> mtyp a) + (acc g (index str (map fst g))) + _ (esym H). +Proof. +subst t. +rewrite {1}/exec. +case: cid => f H. +inversion H; subst g0 str0. +by inj_ex H6; subst f. +Qed. + +Lemma exp_var'E str t (f : find str t) H : exp_var' str f = exp_var str H. +Proof. by rewrite /exp_var'; congr exp_var. Qed. + +Lemma exec_letin g x t1 t2 (e1 : exp g t1) (e2 : exp ((x, t1) :: g) t2) : + exec [let x := e1 in e2] = (fun a => (exec e2) ((exec e1) a, a)). +Proof. by apply/exec_eval/eval_letin; exact: eval_exec. Qed. + +Goal ([{1}:R] : exp [::] _) -e-> (fun=> 1). +Proof. exact: eval_real. Qed. +Goal @eval [::] _ [{1}:R + {2}:R] (fun=> 3). +Proof. exact/eval_plus/eval_real/eval_real. Qed. +Goal @eval [:: ("x", Real)] _ (exp_var "x" erefl) (@acc [:: ("x", Real)] 0). +Proof. exact: eval_var. Qed. +Goal @eval [::] _ [let "x" := {1}:R in #{"x"}] (fun=> 1). +Proof. +apply/exec_eval; rewrite exec_letin/=. +apply/funext => t/=. +by rewrite exp_var'E exec_real/= exec_var/=. +Qed. + +Goal exec (g := [::]) [let "x" := {1}:R in #{"x"}] = (fun=> 1). +Proof. +rewrite exec_letin//=. +apply/funext => x. +by rewrite exp_var'E exec_var/= exec_real. +Qed. + +End eval. + +End with_bidirectional_hints. + +End lang_intrinsic_tysc. +End lang_intrinsic_tysc. diff --git a/theories/lang_syntax_util.v b/theories/lang_syntax_util.v new file mode 100644 index 000000000..ad84918c9 --- /dev/null +++ b/theories/lang_syntax_util.v @@ -0,0 +1,80 @@ +Require Import String. +From HB Require Import structures. +Require Import Classical_Prop. (* NB: to compile with Coq 8.17 *) +From mathcomp Require Import all_ssreflect. +Require Import signed. + +(******************************************************************************) +(* Shared by lang_syntax_*.v files *) +(******************************************************************************) + +Definition string_eqMixin := @EqMixin string String.eqb eqb_spec. +Canonical string_eqType := EqType string string_eqMixin. + +Ltac inj_ex H := revert H; + match goal with + | |- existT ?P ?l (existT ?Q ?t (existT ?R ?u (existT ?T ?v ?v1))) = + existT ?P ?l (existT ?Q ?t (existT ?R ?u (existT ?T ?v ?v2))) -> _ => + (intro H; do 4 apply Classical_Prop.EqdepTheory.inj_pair2 in H) + | |- existT ?P ?l (existT ?Q ?t (existT ?R ?u ?v1)) = + existT ?P ?l (existT ?Q ?t (existT ?R ?u ?v2)) -> _ => + (intro H; do 3 apply Classical_Prop.EqdepTheory.inj_pair2 in H) + | |- existT ?P ?l (existT ?Q ?t ?v1) = + existT ?P ?l (existT ?Q ?t ?v2) -> _ => + (intro H; do 2 apply Classical_Prop.EqdepTheory.inj_pair2 in H) + | |- existT ?P ?l (existT ?Q ?t ?v1) = + existT ?P ?l (existT ?Q ?t' ?v2) -> _ => + (intro H; do 2 apply Classical_Prop.EqdepTheory.inj_pair2 in H) + | |- existT ?P ?l ?v1 = + existT ?P ?l ?v2 -> _ => + (intro H; apply Classical_Prop.EqdepTheory.inj_pair2 in H) + | |- existT ?P ?l ?v1 = + existT ?P ?l' ?v2 -> _ => + (intro H; apply Classical_Prop.EqdepTheory.inj_pair2 in H) +end. + +Set Implicit Arguments. +Unset Strict Implicit. +Set Printing Implicit Defensive. + +Section tagged_context. +Context {T : eqType} {t0 : T}. +Let ctx := seq (string * T). +Implicit Types (str : string) (g : ctx) (t : T). + +Definition dom g := map fst g. + +Definition lookup g str := nth t0 (map snd g) (index str (dom g)). + +Structure tagged_ctx := Tag {untag : ctx}. + +Structure find str t := Find { + ctx_of : tagged_ctx ; + #[canonical=no] ctx_prf : t = lookup (untag ctx_of) str}. + +Lemma ctx_prf_head str t g : t = lookup ((str, t) :: g) str. +Proof. by rewrite /lookup /= !eqxx. Qed. + +Lemma ctx_prf_tail str t g str' t' : + str' != str -> + t = lookup g str -> + t = lookup ((str', t') :: g) str. +Proof. +move=> str'str tg /=; rewrite /lookup/=. +by case: ifPn => //=; rewrite (negbTE str'str). +Qed. + +Definition recurse_tag g := Tag g. +Canonical found_tag g := recurse_tag g. + +Canonical found str t g : find str t := + @Find str t (found_tag ((str, t) :: g)) + (@ctx_prf_head str t g). + +Canonical recurse str t str' t' {H : infer (str' != str)} + (g : find str t) : find str t := + @Find str t (recurse_tag ((str', t') :: untag (ctx_of g))) + (@ctx_prf_tail str t (untag (ctx_of g)) str' t' H (ctx_prf g)). + +End tagged_context. +Arguments lookup {T} t0 g str. diff --git a/theories/measure.v b/theories/measure.v index 0a3b817a0..c41343aa7 100644 --- a/theories/measure.v +++ b/theories/measure.v @@ -1550,8 +1550,8 @@ Lemma measurable_fun_if (g h : T1 -> T2) D (mD : measurable D) measurable_fun D (fun t => if f t then g t else h t). Proof. move=> mx my /= _ B mB; rewrite (_ : _ @^-1` B = - ((f @^-1` [set true]) `&` (g @^-1` B)) `|` - ((f @^-1` [set false]) `&` (h @^-1` B))). + ((f @^-1` [set true]) `&` (g @^-1` B)) `|` + ((f @^-1` [set false]) `&` (h @^-1` B))). rewrite setIUr; apply: measurableU. - by rewrite setIA; apply: mx => //; exact: mf. - by rewrite setIA; apply: my => //; exact: mf. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index bffc1621c..48e78ef85 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -12,14 +12,14 @@ Require Import lebesgue_measure numfun lebesgue_integral exp kernel. (* *) (* bernoulli r1 == Bernoulli probability with r1 a proof that *) (* r : {nonneg R} is smaller than 1 *) -(* *) -(* sample P == sample according to the probability P *) +(* uniform_probability a b ab0 == uniform probability over the interval [a,b] *) +(* sample_cst P == sample according to the probability P *) (* letin l k == execute l, augment the context, and execute k *) (* ret mf == access the context with f and return the result *) (* score mf == observe t from d, where f is the density of d and *) (* t occurs in f *) (* e.g., score (r e^(-r * t)) = observe t from exp(r) *) -(* pnormalize k P == normalize the kernel k into a probability kernel, *) +(* normalize k P == normalize the kernel k into a probability kernel, *) (* P is a default probability in case normalization is *) (* not possible *) (* ite mf k1 k2 == access the context with the boolean function f and *) @@ -41,9 +41,6 @@ Local Open Scope ring_scope. Local Open Scope ereal_scope. (* TODO: PR *) -Lemma onem1' (R : numDomainType) (p : R) : (p + `1- p = 1)%R. -Proof. by rewrite /onem addrCA subrr addr0. Qed. - Lemma onem_nonneg_proof (R : numDomainType) (p : {nonneg R}) : (p%:num <= 1 -> 0 <= `1-(p%:num))%R. Proof. by rewrite /onem/= subr_ge0. Qed. @@ -53,6 +50,39 @@ Definition onem_nonneg (R : numDomainType) (p : {nonneg R}) NngNum (onem_nonneg_proof p1). (* /TODO: PR *) +Lemma invr_nonneg_proof (R : numDomainType) (p : {nonneg R}) : + (0 <= (p%:num)^-1)%R. +Proof. by rewrite invr_ge0. Qed. + +Definition invr_nonneg (R : numDomainType) (p : {nonneg R}) := + NngNum (invr_nonneg_proof p). + +(* TODO: move *) +Lemma eq_probability R d (Y : measurableType d) (m1 m2 : probability Y R) : + (m1 =1 m2 :> (set Y -> \bar R)) -> m1 = m2. +Proof. +move: m1 m2 => [m1 +] [m2 +] /= m1m2. +move/funext : m1m2 => <- -[[c11 c12] [m01] [sf1] [sig1] [fin1] [sub1] [p1]] + [[c21 c22] [m02] [sf2] [sig2] [fin2] [sub2] [p2]]. +have ? : c11 = c21 by []. +subst c21. +have ? : c12 = c22 by []. +subst c22. +have ? : m01 = m02 by []. +subst m02. +have ? : sf1 = sf2 by []. +subst sf2. +have ? : sig1 = sig2 by []. +subst sig2. +have ? : fin1 = fin2 by []. +subst fin2. +have ? : sub1 = sub2 by []. +subst sub2. +have ? : p1 = p2 by []. +subst p2. +by f_equal. +Qed. + Section bernoulli. Variables (R : realType) (p : {nonneg R}) (p1 : (p%:num <= 1)%R). Local Open Scope ring_scope. @@ -69,13 +99,46 @@ Local Close Scope ring_scope. Let bernoulli_setT : bernoulli [set: _] = 1. Proof. rewrite /bernoulli/= /measure_add/= /msum 2!big_ord_recr/= big_ord0 add0e/=. -by rewrite /mscale/= !diracT !mule1 -EFinD onem1'. +by rewrite /mscale/= !diracT !mule1 -EFinD add_onemK. Qed. -HB.instance Definition _ := @Measure_isProbability.Build _ _ R bernoulli bernoulli_setT. +HB.instance Definition _ := + @Measure_isProbability.Build _ _ R bernoulli bernoulli_setT. End bernoulli. +Lemma integral_bernoulli {R : realType} + (p : {nonneg R}) (p1 : (p%:num <= 1)%R) (f : bool -> set bool -> _) U : + (forall x y, 0 <= f x y) -> + \int[bernoulli p1]_y (f y ) U = + p%:num%:E * f true U + (`1-(p%:num))%:E * f false U. +Proof. +move=> f0. +rewrite ge0_integral_measure_sum// 2!big_ord_recl/= big_ord0 adde0/=. +by rewrite !ge0_integral_mscale//= !integral_dirac//= indicT 2!mul1e. +Qed. + +Section uniform_probability. +Context {R : realType}. +Variables (a b : R) (ab0 : (0 < b - a)%R). + +Definition uniform_probability := mscale (invr_nonneg (NngNum (ltW ab0))) + (mrestr lebesgue_measure (measurable_itv `[a, b])). + +HB.instance Definition _ := Measure.on uniform_probability. + +Let uniform_probability_setT : uniform_probability [set: _] = 1. +Proof. +rewrite /uniform_probability /mscale/= /mrestr/=. +rewrite setTI lebesgue_measure_itv hlength_itv/= lte_fin. +by rewrite -subr_gt0 ab0 -EFinD -EFinM mulVf// gt_eqF// subr_gt0. +Qed. + +HB.instance Definition _ := @Measure_isProbability.Build _ _ R + uniform_probability uniform_probability_setT. + +End uniform_probability. + Section mscore. Context d (T : measurableType d) (R : realType). Variable f : T -> R. @@ -283,7 +346,7 @@ by rewrite /mseries eseries0. Qed. #[export] -HB.instance Definition _ t := @Kernel_isSFinite_subdef.Build _ _ _ _ _ +HB.instance Definition _ := @Kernel_isSFinite_subdef.Build _ _ _ _ _ (kiteT k) sfinite_kiteT. End sfkiteT. @@ -298,7 +361,7 @@ by rewrite /= /mzero. Qed. #[export] -HB.instance Definition _ t := Kernel_isFinite.Build _ _ _ _ _ +HB.instance Definition _ := Kernel_isFinite.Build _ _ _ _ _ (kiteT k) kiteT_uub. End fkiteT. @@ -366,6 +429,7 @@ Section ite. Context d d' (T : measurableType d) (T' : measurableType d') (R : realType). Variables (f : T -> bool) (u1 u2 : R.-sfker T ~> T'). +(* NB: not used? *) Definition mite (mf : measurable_fun setT f) : T -> set T' -> \bar R := fun t => if f t then u1 t else u2 t. @@ -405,9 +469,12 @@ Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). Definition ret (f : X -> Y) (mf : measurable_fun setT f) : R.-pker X ~> Y := [the R.-pker _ ~> _ of kdirac mf]. -Definition sample (P : pprobability Y R) : R.-pker X ~> Y := +Definition sample_cst (P : pprobability Y R) : R.-pker X ~> Y := [the R.-pker _ ~> _ of kprobability (measurable_cst P)]. +Definition sample (P : X -> pprobability Y R) (mP : measurable_fun setT P) : R.-pker X ~> Y := + [the R.-pker _ ~> _ of kprobability mP]. + Definition normalize (k : R.-sfker X ~> Y) P : X -> probability Y R := fun x => [the probability _ _ of mnormalize (k x) P]. @@ -417,6 +484,7 @@ Definition ite (f : X -> bool) (mf : measurable_fun setT f) End insn2. Arguments ret {d d' X Y R f} mf. +Arguments sample_cst {d d' X Y R}. Arguments sample {d d' X Y R}. Section insn2_lemmas. @@ -426,7 +494,10 @@ Lemma retE (f : X -> Y) (mf : measurable_fun setT f) x : ret mf x = \d_(f x) :> (_ -> \bar R). Proof. by []. Qed. -Lemma sampleE (P : probability Y R) (x : X) : sample P x = P. +Lemma sample_cstE (P : probability Y R) (x : X) : sample_cst P x = P. +Proof. by []. Qed. + +Lemma sampleE (P : X -> pprobability Y R) (mP : measurable_fun setT P) (x : X) : sample P mP x = P x. Proof. by []. Qed. Lemma normalizeE (f : R.-sfker X ~> Y) P x U : @@ -451,6 +522,15 @@ Qed. End insn2_lemmas. +Lemma normalize_kdirac (R : realType) + d (T : measurableType d) d' (T' : measurableType d') (x : T) (r : T') P : + normalize (kdirac (measurable_cst r)) P x = \d_r :> probability T' R. +Proof. +apply: eq_probability => U. +rewrite normalizeE /= diracE in_setT/=. +by rewrite onee_eq0/= indicE in_setT/= -div1r divr1 mule1. +Qed. + Section insn3. Context d d' d3 (X : measurableType d) (Y : measurableType d') (Z : measurableType d3) (R : realType). @@ -523,13 +603,13 @@ Arguments fail {d d' X Y R}. Module Notations. -Notation var1of2 := (@measurable_fst _ _ _ _). +(*Notation var1of2 := (@measurable_fst _ _ _ _). Notation var2of2 := (@measurable_snd _ _ _ _). Notation var1of3 := (measurableT_comp (@measurable_fst _ _ _ _) (@measurable_fst _ _ _ _)). Notation var2of3 := (measurableT_comp (@measurable_snd _ _ _ _) (@measurable_fst _ _ _ _)). -Notation var3of3 := (@measurable_snd _ _ _ _). +Notation var3of3 := (@measurable_snd _ _ _ _).*) Notation mR := Real_sort__canonical__measure_Measurable. Notation munit := Datatypes_unit__canonical__measure_Measurable. @@ -544,12 +624,249 @@ Definition kr (r : R) := @measurable_cst _ _ T _ setT r. Definition k3 : measurable_fun _ _ := kr 3%:R. Definition k10 : measurable_fun _ _ := kr 10%:R. Definition ktt := @measurable_cst _ _ T _ setT tt. +Definition kb (b : bool) := @measurable_cst _ _ T _ setT b. End cst_fun. Arguments kr {d T R}. Arguments k3 {d T R}. Arguments k10 {d T R}. Arguments ktt {d T}. +Arguments kb {d T}. + +Section iter_mprod. +Import Notations. + +Fixpoint iter_mprod (l : list {d & measurableType d}) + : {d & measurableType d} := + match l with + | [::] => existT measurableType _ munit + | h :: t => let t' := iter_mprod t in + existT _ _ [the measurableType (projT1 h, projT1 t').-prod of + (projT2 h * projT2 t')%type] + end. + +End iter_mprod. + +Section acc. +Import Notations. +Context {R : realType}. + +Fixpoint acc (l : seq {d & measurableType d}) n : + projT2 (iter_mprod l) -> projT2 (nth (existT _ _ munit) l n) := + match l return + projT2 (iter_mprod l) -> projT2 (nth (existT _ _ munit) l n) + with + | [::] => match n with | O => id | m.+1 => id end + | _ :: _ => match n with + | O => fst + | m.+1 => fun H => acc m H.2 + end + end. + +Lemma measurable_acc (l : seq {d & measurableType d}) n : + measurable_fun setT (@acc l n). +Proof. +by elim: l n => //= h t ih [|m] //; exact: (measurableT_comp (ih _)). +Qed. +End acc. +Arguments acc : clear implicits. +Arguments measurable_acc : clear implicits. + +Section rpair_pairA. +Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) + (T2 : measurableType d2). + +Definition rpair d (T : measurableType d) t : + ([the measurableType _ of T0] -> [the measurableType _ of T0 * T])%type := + fun x => (x, t). + +Lemma mrpair d (T : measurableType d) t : measurable_fun setT (@rpair _ T t). +Proof. exact: measurable_fun_prod. Qed. + +Definition pairA : ([the measurableType _ of T0 * T1 * T2] -> + [the measurableType _ of T0 * (T1 * T2)])%type := + fun x => (x.1.1, (x.1.2, x.2)). + +Definition mpairA : measurable_fun setT pairA. +Proof. +apply: measurable_fun_prod => /=; first exact: measurableT_comp. +by apply: measurable_fun_prod => //=; exact: measurableT_comp. +Qed. + +Definition pairAi : ([the measurableType _ of T0 * (T1 * T2)] -> + [the measurableType _ of T0 * T1 * T2])%type := + fun x => (x.1, x.2.1, x.2.2). + +Definition mpairAi : measurable_fun setT pairAi. +Proof. +apply: measurable_fun_prod => //=; last exact: measurableT_comp. +apply: measurable_fun_prod => //=; exact: measurableT_comp. +Qed. + +End rpair_pairA. +Arguments rpair {d0 T0 d} T. +#[global] Hint Extern 0 (measurable_fun _ (rpair _ _)) => + solve [apply: mrpair] : core. +Arguments pairA {d0 d1 d2 T0 T1 T2}. +#[global] Hint Extern 0 (measurable_fun _ pairA) => + solve [apply: mpairA] : core. +Arguments pairAi {d0 d1 d2 T0 T1 T2}. +#[global] Hint Extern 0 (measurable_fun _ pairAi) => + solve [apply: mpairAi] : core. + +Section rpair_pairA_comp. +Import Notations. +Context d0 d1 d2 d3 (T0 : measurableType d0) (T1 : measurableType d1) + (T2 : measurableType d2) (T3 : measurableType d3) (R : realType). + +Definition pairAr d (T : measurableType d) t : + ([the measurableType _ of T0 * T1] -> + [the measurableType _ of T0 * (T1 * T)])%type := + pairA \o rpair T t. +Arguments pairAr {d} T. + +Lemma mpairAr d (T : measurableType d) t : measurable_fun setT (pairAr T t). +Proof. exact: measurableT_comp. Qed. + +Definition pairAAr : ([the measurableType _ of T0 * T1 * T2] -> + [the measurableType _ of T0 * (T1 * (T2 * munit))])%type := + pairA \o pairA \o rpair munit tt. + +Lemma mpairAAr : measurable_fun setT pairAAr. +Proof. by do 2 apply: measurableT_comp => //. Qed. + +Definition pairAAAr : ([the measurableType _ of T0 * T1 * T2 * T3] -> + [the measurableType _ of T0 * (T1 * (T2 * (T3 * munit)))])%type := + pairA \o pairA \o pairA \o rpair munit tt. + +Lemma mpairAAAr : measurable_fun setT pairAAAr. +Proof. by do 3 apply: measurableT_comp => //. Qed. + +Definition pairAArAi : ([the measurableType _ of T0 * (T1 * T2)] -> + [the measurableType _ of T0 * (T1 * (T2 * munit))])%type := + pairAAr \o pairAi. + +Lemma mpairAArAi : measurable_fun setT pairAArAi. +Proof. by apply: measurableT_comp => //=; exact: mpairAAr. Qed. + +Definition pairAAArAAi : ([the measurableType _ of T3 * (T0 * (T1 * T2))] -> + [the measurableType _ of T3 * (T0 * (T1 * (T2 * munit)))])%type := + pairA \o pairA \o pairA \o rpair munit tt \o pairAi \o pairAi. + +Lemma mpairAAARAAAi : measurable_fun setT pairAAArAAi. +Proof. by do 5 apply: measurableT_comp => //=. Qed. + +End rpair_pairA_comp. +Arguments pairAr {d0 d1 T0 T1 d} T. +Arguments pairAAr {d0 d1 d2 T0 T1 T2}. +Arguments pairAAAr {d0 d1 d2 d3 T0 T1 T2 T3}. +Arguments pairAArAi {d0 d1 d2 T0 T1 T2}. +Arguments pairAAArAAi {d0 d1 d2 d3 T0 T1 T2 T3}. + +Section accessor_functions. +Import Notations. +Context d0 d1 d2 d3 (T0 : measurableType d0) (T1 : measurableType d1) + (T2 : measurableType d2) (T3 : measurableType d3) (R : realType). + +Definition Of2 := [:: existT _ _ T0; existT _ _ T1]. + +Definition acc0of2 : [the measurableType _ of (T0 * T1)%type] -> T0 := + @acc Of2 0 \o pairAr munit tt. + +Lemma macc0of2 : measurable_fun setT acc0of2. +Proof. +by apply: measurableT_comp; [exact: (measurable_acc Of2 0)|exact: mpairAr]. +Qed. + +Definition acc1of2 : [the measurableType _ of (T0 * T1)%type] -> T1 := + acc Of2 1 \o pairAr munit tt. + +Lemma macc1of2 : measurable_fun setT acc1of2. +Proof. +by apply: measurableT_comp; [exact: (measurable_acc Of2 1)|exact: mpairAr]. +Qed. + +Definition Of3 := [:: existT _ _ T0; existT _ _ T1; existT _ d2 T2]. + +Definition acc1of3 : [the measurableType _ of (T0 * T1 * T2)%type] -> T1 := + acc Of3 1 \o pairAAr. + +Lemma macc1of3 : measurable_fun setT acc1of3. +Proof. +by apply: measurableT_comp; [exact: (measurable_acc Of3 1)|exact: mpairAAr]. +Qed. + +Definition acc2of3 : [the measurableType _ of (T0 * T1 * T2)%type] -> T2 := + acc Of3 2 \o pairAAr. + +Lemma macc2of3 : measurable_fun setT acc2of3. +Proof. +by apply: measurableT_comp; [exact: (measurable_acc Of3 2)|exact: mpairAAr]. +Qed. + +Definition acc0of3' : [the measurableType _ of (T0 * (T1 * T2))%type] -> T0 := + acc Of3 0 \o pairAArAi. + +Lemma macc0of3' : measurable_fun setT acc0of3'. +Proof. +by apply: measurableT_comp; [exact: (measurable_acc Of3 0)|exact: mpairAArAi]. +Qed. + +Definition acc1of3' : [the measurableType _ of (T0 * (T1 * T2))%type] -> T1 := + acc Of3 1 \o pairAArAi. + +Lemma macc1of3' : measurable_fun setT acc1of3'. +Proof. +by apply: measurableT_comp; [exact: (measurable_acc Of3 1)|exact: mpairAArAi]. +Qed. + +Definition acc2of3' : [the measurableType _ of (T0 * (T1 * T2))%type] -> T2 := + acc Of3 2 \o pairAArAi. + +Lemma macc2of3' : measurable_fun setT acc2of3'. +Proof. +by apply: measurableT_comp; [exact: (measurable_acc Of3 2)|exact: mpairAArAi]. +Qed. + +Definition Of4 := + [:: existT _ _ T0; existT _ _ T1; existT _ d2 T2; existT _ d3 T3]. + +Definition acc1of4 : [the measurableType _ of (T0 * T1 * T2 * T3)%type] -> T1 := + acc Of4 1 \o pairAAAr. + +Lemma macc1of4 : measurable_fun setT acc1of4. +Proof. +by apply: measurableT_comp; [exact: (measurable_acc Of4 1)|exact: mpairAAAr]. +Qed. + +Definition acc2of4' : + [the measurableType _ of (T0 * (T1 * (T2 * T3)))%type] -> T2 := + acc Of4 2 \o pairAAArAAi. + +Lemma macc2of4' : measurable_fun setT acc2of4'. +Proof. +by apply: measurableT_comp; [exact: (measurable_acc Of4 2)|exact: mpairAAARAAAi]. +Qed. + +Definition acc3of4 : [the measurableType _ of (T0 * T1 * T2 * T3)%type] -> T3 := + acc Of4 3 \o pairAAAr. + +Lemma macc3of4 : measurable_fun setT acc3of4. +Proof. +by apply: measurableT_comp; [exact: (measurable_acc Of4 3)|exact: mpairAAAr]. +Qed. + +End accessor_functions. +Arguments macc0of2 {d0 d1 _ _}. +Arguments macc1of2 {d0 d1 _ _}. +Arguments macc0of3' {d0 d1 d2 _ _ _}. +Arguments macc1of3 {d0 d1 d2 _ _ _}. +Arguments macc1of3' {d0 d1 d2 _ _ _}. +Arguments macc2of3 {d0 d1 d2 _ _ _}. +Arguments macc2of3' {d0 d1 d2 _ _ _}. +Arguments macc1of4 {d0 d1 d2 d3 _ _ _ _}. +Arguments macc2of4' {d0 d1 d2 d3 _ _ _ _}. +Arguments macc3of4 {d0 d1 d2 d3 _ _ _ _}. Section insn1_lemmas. Import Notations. @@ -567,10 +884,12 @@ Qed. Lemma scoreE d' (T' : measurableType d') (x : T * T') (U : set T') (f : R -> R) (r : R) (r0 : (0 <= r)%R) (f0 : (forall r, 0 <= r -> 0 <= f r)%R) (mf : measurable_fun setT f) : - score (measurableT_comp mf var2of2) + score (measurableT_comp mf (@macc1of2 _ _ _ _)) (x, r) (curry (snd \o fst) x @^-1` U) = (f r)%:E * \d_x.2 U. -Proof. by rewrite /score/= /mscale/= ger0_norm// f0. Qed. +Proof. +by rewrite /score/= /mscale/= ger0_norm//= f0. +Qed. Lemma score_score (f : R -> R) (g : R * unit -> R) (mf : measurable_fun setT f) @@ -583,13 +902,11 @@ rewrite {1}/letin; unlock. by rewrite kcomp_scoreE/= /mscale/= diracE normrM muleA EFinM. Qed. -Import Notations. - (* hard constraints to express score below 1 *) Lemma score_fail (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : score (kr r%:num) = - letin (sample [the probability _ _ of bernoulli r1] : R.-pker T ~> _) - (ite var2of2 (ret ktt) fail). + letin (sample_cst (bernoulli r1) : R.-pker T ~> _) + (ite (@macc1of2 _ _ _ _) (ret ktt) fail). Proof. apply/eq_sfkernel => x U. rewrite letinE/= /sample; unlock. @@ -603,7 +920,8 @@ End insn1_lemmas. Section letin_ite. Context d d2 d3 (T : measurableType d) (T2 : measurableType d2) (Z : measurableType d3) (R : realType). -Variables (k1 k2 : R.-sfker T ~> Z) (u : R.-sfker [the measurableType _ of (T * Z)%type] ~> T2) +Variables (k1 k2 : R.-sfker T ~> Z) + (u : R.-sfker [the measurableType _ of (T * Z)%type] ~> T2) (f : T -> bool) (mf : measurable_fun setT f) (t : T) (U : set T2). @@ -693,10 +1011,10 @@ HB.instance Definition _ z := @Measure_isSFinite_subdef.Build _ Y R Lemma letinC z A : measurable A -> letin t (letin u' - (ret (measurable_fun_prod var2of3 var3of3))) z A = + (ret (measurable_fun_prod macc1of3 macc2of3))) z A = letin u (letin t' - (ret (measurable_fun_prod var3of3 var2of3))) z A. + (ret (measurable_fun_prod macc2of3 macc1of3))) z A. Proof. move=> mA. rewrite !letinE. @@ -746,29 +1064,36 @@ End constants. Arguments p12 {R}. Arguments p14 {R}. Arguments p27 {R}. +Arguments p1S {R}. Section poisson. Variable R : realType. Local Open Scope ring_scope. (* density function for Poisson *) -Definition poisson k r : R := r ^+ k / k`!%:R^-1 * expR (- r). +Definition poisson k r : R := + if r > 0 then r ^+ k / k`!%:R^-1 * expR (- r) else 1%:R. -Lemma poisson_ge0 k r : 0 <= r -> 0 <= poisson k r. +Lemma poisson_ge0 k r : 0 <= poisson k r. Proof. -move=> r0; rewrite /poisson mulr_ge0 ?expR_ge0//. -by rewrite mulr_ge0// exprn_ge0. +rewrite /poisson; case: ifPn => r0//. +by rewrite mulr_ge0 ?expR_ge0// mulr_ge0// exprn_ge0 ?ltW. Qed. Lemma poisson_gt0 k r : 0 < r -> 0 < poisson k.+1 r. Proof. -move=> r0; rewrite /poisson mulr_gt0 ?expR_gt0//. +move=> r0; rewrite /poisson r0 mulr_gt0 ?expR_gt0//. by rewrite divr_gt0// ?exprn_gt0// invr_gt0 ltr0n fact_gt0. Qed. -Lemma mpoisson k : measurable_fun setT (poisson k). +Lemma measurable_poisson k : measurable_fun setT (poisson k). Proof. -by apply: measurable_funM => /=; [exact: measurable_funM|exact: measurableT_comp]. +rewrite /poisson; apply: measurable_fun_if => //. + apply: (measurable_fun_bool true). + rewrite (_ : _ @^-1` _ = `]0, +oo[%classic)//. + by apply/seteqP; split => x /=; rewrite in_itv/= andbT. +by apply: measurable_funM => /=; + [exact: measurable_funM|exact: measurableT_comp]. Qed. Definition poisson3 := poisson 4 3%:R. (* 0.168 *) @@ -800,7 +1125,7 @@ End exponential. Lemma letin_sample_bernoulli d d' (T : measurableType d) (T' : measurableType d') (R : realType)(r : {nonneg R}) (r1 : (r%:num <= 1)%R) (u : R.-sfker [the measurableType _ of (T * bool)%type] ~> T') x y : - letin (sample [the probability _ _ of bernoulli r1]) u x y = + letin (sample_cst (bernoulli r1)) u x y = r%:num%:E * u (x, true) y + (`1- (r%:num))%:E * u (x, false) y. Proof. rewrite letinE/=. @@ -814,8 +1139,8 @@ Context d (T : measurableType d) (R : realType). Definition sample_and_return : R.-sfker T ~> _ := letin - (sample [the probability _ _ of bernoulli p27]) (* T -> B *) - (ret var2of2) (* T * B -> B *). + (sample_cst [the probability _ _ of bernoulli p27]) (* T -> B *) + (ret macc1of2) (* T * B -> B *). Lemma sample_and_returnE t U : sample_and_return t U = (2 / 7%:R)%:E * \d_true U + (5%:R / 7%:R)%:E * \d_false U. @@ -836,8 +1161,8 @@ Context d (T : measurableType d) (R : realType). Definition sample_and_branch : R.-sfker T ~> mR R := letin - (sample [the probability _ _ of bernoulli p27]) (* T -> B *) - (ite var2of2 (ret k3) (ret k10)). + (sample_cst [the probability _ _ of bernoulli p27]) (* T -> B *) + (ite macc1of2 (ret k3) (ret k10)). Lemma sample_and_branchE t U : sample_and_branch t U = (2 / 7%:R)%:E * \d_(3%:R : R) U + @@ -859,7 +1184,7 @@ Lemma measurable_fun_mand (x y : T * mbool * mbool -> mbool) : measurable_fun setT x -> measurable_fun setT y -> measurable_fun setT (mand x y). Proof. -move=> /= mx my; apply: (@measurable_fun_bool _ _ _ _ true). +move=> /= mx my; apply: (measurable_fun_bool true). rewrite [X in measurable X](_ : _ = (x @^-1` [set true]) `&` (y @^-1` [set true])); last first. by rewrite /mand; apply/seteqP; split => z/= /andP. @@ -869,13 +1194,13 @@ apply: measurableI. Qed. Definition bernoulli_and : R.-sfker T ~> mbool := - (letin (sample [the probability _ _ of bernoulli p12]) - (letin (sample [the probability _ _ of bernoulli p12]) - (ret (measurable_fun_mand var2of3 var3of3)))). + (letin (sample_cst [the probability _ _ of bernoulli p12]) + (letin (sample_cst [the probability _ _ of bernoulli p12]) + (ret (measurable_fun_mand macc1of3 macc2of3)))). Lemma bernoulli_andE t U : bernoulli_and t U = - sample [the probability _ _ of bernoulli p14] t U. + sample_cst (bernoulli p14) t U. Proof. rewrite /bernoulli_and 3!letin_sample_bernoulli/= /mand/= muleDr//= -muleDl//. rewrite !muleA -addeA -muleDl// -!EFinM !onem1S/= -splitr mulr1. @@ -893,11 +1218,11 @@ Import Notations. Context d (T : measurableType d) (R : realType) (h : R -> R). Hypothesis mh : measurable_fun setT h. Definition kstaton_bus : R.-sfker T ~> mbool := - letin (sample [the probability _ _ of bernoulli p27]) + letin (sample_cst [the probability _ _ of bernoulli p27]) (letin - (letin (ite var2of2 (ret k3) (ret k10)) - (score (measurableT_comp mh var3of3))) - (ret var2of3)). + (letin (ite macc1of2 (ret k3) (ret k10)) + (score (measurableT_comp mh macc2of3))) + (ret macc1of3)). Definition staton_bus := normalize kstaton_bus. @@ -911,7 +1236,7 @@ Section staton_bus_poisson. Import Notations. Context d (T : measurableType d) (R : realType). Let poisson4 := @poisson R 4%N. -Let mpoisson4 := @mpoisson R 4%N. +Let mpoisson4 := @measurable_poisson R 4%N. Definition kstaton_bus_poisson : R.-sfker (mR R) ~> mbool := kstaton_bus _ mpoisson4. @@ -1001,3 +1326,574 @@ by rewrite addr_gt0// mulr_gt0//= ?divr_gt0// ?ltr0n// exp_density_gt0 ?ltr0n. Qed. End staton_bus_exponential. + +(* X + Y is a measurableType if X and Y are *) +Canonical sum_pointedType (X Y : pointedType) := + PointedType (X + Y) (@inl X Y point). + +Section measurable_sum. +Context d d' (X : measurableType d) (Y : measurableType d'). + +Definition measurable_sum : set (set (X + Y)) := setT. + +Let sum0 : measurable_sum set0. Proof. by []. Qed. + +Let sumC A : measurable_sum A -> measurable_sum (~` A). Proof. by []. Qed. + +Let sumU (F : (set (X + Y))^nat) : (forall i, measurable_sum (F i)) -> + measurable_sum (\bigcup_i F i). +Proof. by []. Qed. + +HB.instance Definition _ := @isMeasurable.Build default_measure_display (X + Y)%type + (Pointed.class _) measurable_sum sum0 sumC sumU. + +End measurable_sum. + +Lemma measurable_fun_sum dA dB d' (A : measurableType dA) (B : measurableType dB) + (Y : measurableType d') (f : A -> Y) (g : B -> Y) : + measurable_fun setT f -> measurable_fun setT g -> + measurable_fun setT (fun tb : A + B => + match tb with inl a => f a | inr b => g b end). +Proof. +move=> mx my/= _ Z mZ /=; rewrite setTI /=. +rewrite (_ : _ @^-1` Z = inl @` (f @^-1` Z) `|` inr @` (g @^-1` Z)). + exact: measurableU. +apply/seteqP; split. + by move=> [a Zxa|b Zxb]/=; [left; exists a|right; exists b]. +by move=> z [/= [a Zxa <-//=]|]/= [b Zyb <-//=]. +Qed. + +(* TODO: measurable_fun_if_pair -> measurable_fun_if_pair_bool? *) +Lemma measurable_fun_if_pair_nat d d' (X : measurableType d) + (Y : measurableType d') (f g : X -> Y) (n : nat) : + measurable_fun setT f -> measurable_fun setT g -> + measurable_fun setT (fun xn => if xn.2 == n then f xn.1 else g xn.1). +Proof. +move=> mx my; apply: measurable_fun_ifT => //=. +- have h : measurable_fun [set: nat] (fun t => t == n) by []. + exact: (@measurableT_comp _ _ _ _ _ _ _ _ _ h). +- exact: measurableT_comp. +- exact: measurableT_comp. +Qed. + +Module CASE_NAT. +Section case_nat. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). + +Section case_nat_ker. +Variable k : R.-ker X ~> Y. + +Definition case_nat_ j : X * nat -> {measure set Y -> \bar R} := + fun xn => if xn.2 == j then k xn.1 else [the measure _ _ of mzero]. + +Let measurable_fun_case_nat_ m U : measurable U -> + measurable_fun setT (case_nat_ m ^~ U). +Proof. +move=> mU; rewrite /case_nat_ (_ : (fun _ => _) = + (fun x => if x.2 == m then k x.1 U else mzero U)) /=; last first. + by apply/funext => -[t b]/=; case: ifPn. +apply: (@measurable_fun_if_pair_nat _ _ _ _ (k ^~ U) (fun=> mzero U)) => //. +exact/measurable_kernel. +Qed. + +#[export] +HB.instance Definition _ j := isKernel.Build _ _ _ _ _ + (case_nat_ j) (measurable_fun_case_nat_ j). +End case_nat_ker. + +Section sfcase_nat. +Variable k : R.-sfker X ~> Y. + +Let sfcase_nat_ j : exists2 k_ : (R.-ker _ ~> _)^nat, + forall n, measure_fam_uub (k_ n) & + forall x U, measurable U -> case_nat_ k j x U = mseries (k_ ^~ x) 0 U. +Proof. +have [k_ hk /=] := sfinite_kernel k. +exists (fun n => [the _.-ker _ ~> _ of case_nat_ (k_ n) j]) => /=. + move=> n; have /measure_fam_uubP[r k_r] := measure_uub (k_ n). + exists r%:num => /= -[x [|n']]; rewrite /case_nat_//= /mzero//. + by case: ifPn => //= ?; rewrite /mzero. + by case: ifPn => // ?; rewrite /= /mzero. +move=> [x b] U mU; rewrite /case_nat_; case: ifPn => hb; first by rewrite hk. +by rewrite /mseries eseries0. +Qed. + +#[export] +HB.instance Definition _ j := @Kernel_isSFinite_subdef.Build _ _ _ _ _ + (case_nat_ k j) (sfcase_nat_ j). +End sfcase_nat. + +Section fkcase_nat. +Variable k : R.-fker X ~> Y. + +Let case_nat_uub (m : nat) : measure_fam_uub (case_nat_ k m). +Proof. +have /measure_fam_uubP[M hM] := measure_uub k. +exists M%:num => /= -[]; rewrite /case_nat_ => t [|m']/=. + by case: ifPn => //= ?; rewrite /mzero//=. +by case: ifPn => //= ?; rewrite /mzero//=. +Qed. + +#[export] +HB.instance Definition _ j := Kernel_isFinite.Build _ _ _ _ _ + (case_nat_ k j) (case_nat_uub j). +End fkcase_nat. + +End case_nat. +End CASE_NAT. + +Import CASE_NAT. + +Section case_nat. +Context d d' (T : measurableType d) (T' : measurableType d') (R : realType). + +Import CASE_NAT. + +(* case analysis on the nat datatype *) +Definition case_nat (t : R.-sfker T ~> nat) (u_ : (R.-sfker T ~> T')^nat) + : R.-sfker T ~> T' := + [the R.-sfker T ~> nat of t] \; + [the R.-sfker T * nat ~> T' of + kseries (fun n => [the R.-sfker T * nat ~> T' of case_nat_ (u_ n) n])]. + +End case_nat. + +Definition measure_sum_display : + (measure_display * measure_display) -> measure_display. +Proof. exact. Qed. + +Definition image_classes d1 d2 + (T1 : measurableType d1) (T2 : measurableType d2) (T : Type) + (f1 : T1 -> T) (f2 : T2 -> T) := + <>. + +Section sum_salgebra_instance. +Context d1 d2 (T1 : measurableType d1) (T2 : measurableType d2). +Let f1 : T1 -> T1 + T2 := @inl T1 T2. +Let f2 : T2 -> T1 + T2 := @inr T1 T2. + +Lemma sum_salgebra_set0 : image_classes f1 f2 (set0 : set (T1 + T2)). +Proof. exact: sigma_algebra0. Qed. + +Lemma sum_salgebra_setC A : image_classes f1 f2 A -> + image_classes f1 f2 (~` A). +Proof. exact: sigma_algebraC. Qed. + +Lemma sum_salgebra_bigcup (F : _^nat) : (forall i, image_classes f1 f2 (F i)) -> + image_classes f1 f2 (\bigcup_i (F i)). +Proof. exact: sigma_algebra_bigcup. Qed. + +HB.instance Definition sum_salgebra_mixin := + @isMeasurable.Build (measure_sum_display (d1, d2)) + (T1 + T2)%type (Pointed.class _) (image_classes f1 f2) + (sum_salgebra_set0) (sum_salgebra_setC) (sum_salgebra_bigcup). + +End sum_salgebra_instance. +Reserved Notation "p .-sum" (at level 1, format "p .-sum"). +Reserved Notation "p .-sum.-measurable" + (at level 2, format "p .-sum.-measurable"). +Notation "p .-sum" := (measure_sum_display p) : measure_display_scope. +Notation "p .-sum.-measurable" := + ((p.-sum).-measurable : set (set (_ + _))) : + classical_set_scope. + +Module CASE_SUM. + +Section case_suml. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). +Let A : measurableType _ := unit. + +Section kcase_suml. +Variable k : R.-ker X ~> Y. + +Definition case_suml (a : A) : X * A -> {measure set Y -> \bar R} := + fun xa => k xa.1. + +Let measurable_fun_case_suml a U : measurable U -> + measurable_fun setT (case_suml a ^~ U). +Proof. +move=> /= mU; rewrite /case_suml. +have h := measurable_kernel k _ mU. +rewrite (_ : (fun x : X * unit => k x.1 U) = (fun x : X => k x U) \o fst) //. +by apply: measurableT_comp => //. +Qed. + +#[export] +HB.instance Definition _ a := isKernel.Build _ _ _ _ _ + (case_suml a) (measurable_fun_case_suml a). +End kcase_suml. + +Section sfkcase_suml. +Variable k : R.-sfker X ~> Y. + +Let sfinite_case_suml (a : A) : exists2 k_ : (R.-ker _ ~> _)^nat, + forall n, measure_fam_uub (k_ n) & + forall x U, measurable U -> case_suml k a x U = mseries (k_ ^~ x) 0 U. +Proof. +have [k_ hk /=] := sfinite_kernel k. +exists (fun n => [the _.-ker _ ~> _ of case_suml (k_ n) a]) => /=. + move=> n; have /measure_fam_uubP[r k_r] := measure_uub (k_ n). + by exists r%:num => /= -[x []]; rewrite /case_suml//= /mzero//. +move=> [x b] U mU; rewrite /case_suml /=. +by rewrite /mseries hk. +Qed. + +#[export] +HB.instance Definition _ (a : A) := @Kernel_isSFinite_subdef.Build _ _ _ _ _ + (case_suml k a) (sfinite_case_suml a). +End sfkcase_suml. + +Section fkcase_suml. +Variable k : R.-fker X ~> Y. + +Let case_suml_uub (a : A) : measure_fam_uub (case_suml k a). +Proof. +have /measure_fam_uubP[M hM] := measure_uub k. +by exists M%:num => /= -[]; rewrite /case_suml. +Qed. + +#[export] +HB.instance Definition _ a := Kernel_isFinite.Build _ _ _ _ _ + (case_suml k a) (case_suml_uub a). +End fkcase_suml. + +End case_suml. + +Section case_sumr. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). +Let B : measurableType _ := bool. + +Section kcase_sumr. +Variable k : R.-ker X ~> Y. + +Definition case_sumr (b : B) : X * B -> {measure set Y -> \bar R} := + fun xa => if xa.2 == b then k xa.1 else [the measure _ _ of mzero]. + +Let measurable_fun_case_sumr b U : measurable U -> + measurable_fun setT (case_sumr b ^~ U). +Proof. +move=> /= mU; rewrite /case_suml. +have h := measurable_kernel k _ mU. +rewrite /case_sumr. +rewrite (_ : (fun x : X * bool => case_sumr b x U) = + (fun x : X * bool => (if x.2 == b then k x.1 U else [the {measure set Y -> \bar R} of mzero] U))); last first. + apply/funext => x. + rewrite /case_sumr. + by case: ifPn. +apply: measurable_fun_ifT => //=. + rewrite (_ : (fun t : X * bool => t.2 == b) = (fun t : bool => t == b) \o snd)//. + apply: measurableT_comp => //. +rewrite (_ : (fun t : X * bool => k t.1 U) = (fun t : X => k t U) \o fst)//. +by apply: measurableT_comp => //. +Qed. + +#[export] +HB.instance Definition _ b := isKernel.Build _ _ _ _ _ + (case_sumr b) (measurable_fun_case_sumr b). +End kcase_sumr. + +Section sfkcase_sumr. +Variable k : R.-sfker X ~> Y. + +Let sfinite_case_sumr b : exists2 k_ : (R.-ker _ ~> _)^nat, + forall n, measure_fam_uub (k_ n) & + forall x U, measurable U -> case_sumr k b x U = mseries (k_ ^~ x) 0 U. +Proof. +have [k_ hk /=] := sfinite_kernel k. +exists (fun n => [the _.-ker _ ~> _ of case_sumr (k_ n) b]) => /=. + move=> n; have /measure_fam_uubP[r k_r] := measure_uub (k_ n). + by exists r%:num => /= -[x []]; rewrite /case_sumr//=; case: ifPn => // _; + rewrite /= (le_lt_trans _ (k_r x))// /mzero//. +move=> [x [|]] U mU; rewrite /case_sumr /=; case: b => //=; rewrite ?hk//; +by rewrite /mseries/= eseries0. +Qed. + +#[export] +HB.instance Definition _ b := @Kernel_isSFinite_subdef.Build _ _ _ _ _ + (case_sumr k b) (sfinite_case_sumr b). +End sfkcase_sumr. + +Section fkcase_sumr. +Variable k : R.-fker X ~> Y. + +Let case_sumr_uub b : measure_fam_uub (case_sumr k b). +Proof. +have /measure_fam_uubP[M hM] := measure_uub k. +by exists M%:num => /= -[]; rewrite /case_sumr => x [|] /=; case: b => //=; + rewrite (le_lt_trans _ (hM x))// /mzero. +Qed. + +#[export] +HB.instance Definition _ b := Kernel_isFinite.Build _ _ _ _ _ + (case_sumr k b) (case_sumr_uub b). +End fkcase_sumr. + +End case_sumr. +End CASE_SUM. + +Section case_sum'. + +Section kcase_sum'. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). +Let A : measurableType _ := unit. +Let B : measurableType _ := bool. +Variables (k : (A + B)%type -> R.-sfker X ~> Y). + +Definition case_sum' : X * (A + B)%type -> {measure set Y -> \bar R} := + fun xab => match xab with + | (x, inl a) => CASE_SUM.case_suml (k xab.2) a (x, a) + | (x, inr b) => CASE_SUM.case_sumr (k xab.2) b (x, b) + end. + +Let measurable_fun_case_sum' U : measurable U -> + measurable_fun setT (case_sum' ^~ U). +Proof. +rewrite /= => mU. +apply: (measurability (ErealGenInftyO.measurableE R)) => //. +move=> /= _ [_ [x ->] <-]; apply: measurableI => //. +rewrite /case_sum' /CASE_SUM.case_suml /CASE_SUM.case_sumr /=. +rewrite (_ : + (fun x : X * (unit + bool) => (let (x0, s) := x in + match s with inl _ => k x.2 x0 | inr b => if b == b then k x.2 x0 else mzero + end) U) = + (fun x : X * (unit + bool) => k x.2 x.1 U)); last first. + by apply/funext => -[x1 [a|b]] //; rewrite eqxx. +rewrite (_ : _ @^-1` _ = + ([set x1 | k (inl tt) x1 U < x%:E] `*` inl @` [set tt]) `|` + ([set x1 | k (inr false) x1 U < x%:E] `*` inr @` [set false]) `|` + ([set x1 | k (inr true) x1 U < x%:E] `*` inr @` [set true])); last first. + apply/seteqP; split. + - move=> z /=; rewrite in_itv/=; move: z => [z [[]|[|]]]//= ?. + + by do 2 left; split => //; exists tt. + + by right; split => //; exists true. + + by left; right; split => //; exists false. + - move=> z /=; rewrite in_itv/=; move: z => [z [[]|[|]]]//=. + - move=> [[[]//|]|]. + + by move=> [_ []]. + + by move=> [_ []]. + - move=> [[|]|[]//]. + + by move=> [_ []]. + + by move=> [_ [] [|]]. + - move=> [[|[]//]|]. + + by move=> [_ []]. + + by move=> [_ [] [|]]. +pose h1 := [set xub : X * (unit + bool) | k (inl tt) xub.1 U < x%:E]. +have mh1 : measurable h1. + rewrite -[X in measurable X]setTI; apply: emeasurable_fun_infty_o => //=. + have H : measurable_fun [set: X] (fun x => k (inl tt) x U) by exact/measurable_kernel. + move=> _ /= C mC; rewrite setTI. + have := H measurableT _ mC; rewrite setTI => {}H. + rewrite [X in measurable X](_ : _ = ((fun x => k (inl tt) x U) @^-1` C) `*` setT)//. + exact: measurableM. + by apply/seteqP; split => [z//=| z/= []]. +set h2 := [set xub : X * (unit + bool)| k (inr false) xub.1 U < x%:E]. +have mh2 : measurable h2. + rewrite -[X in measurable X]setTI. + apply: emeasurable_fun_infty_o => //=. + have H : measurable_fun [set: X] (fun x => k (inr false) x U) by exact/measurable_kernel. + move=> _ /= C mC; rewrite setTI. + have := H measurableT _ mC; rewrite setTI => {}H. + rewrite [X in measurable X](_ : _ = ((fun x => k (inr false) x U) @^-1` C) `*` setT)//. + exact: measurableM. + by apply/seteqP; split => [z //=|z/= []]. +set h3 := [set xub : X * (unit + bool)| k (inr true) xub.1 U < x%:E]. +have mh3 : measurable h3. + rewrite -[X in measurable X]setTI. + apply: emeasurable_fun_infty_o => //=. + have H : measurable_fun [set: X] (fun x => k (inr true) x U) by exact/measurable_kernel. + move=> _ /= C mC; rewrite setTI. + have := H measurableT _ mC; rewrite setTI => {}H. + rewrite [X in measurable X](_ : _ = ((fun x => k (inr true) x U) @^-1` C) `*` setT)//. + exact: measurableM. + by apply/seteqP; split=> [z//=|z/= []]. +apply: measurableU. +- apply: measurableU. + + apply: measurableM => //. + rewrite [X in measurable X](_ : _ = ysection h1 (inl tt))//. + * by apply: measurable_ysection. + * by apply/seteqP; split => z /=; rewrite /ysection /= inE. + + apply: measurableM => //. + rewrite [X in measurable X](_ : _ = ysection h2 (inr false))//. + * by apply: measurable_ysection. + * by apply/seteqP; split => z /=; rewrite /ysection /= inE. +- apply: measurableM => //. + rewrite [X in measurable X](_ : _ = ysection h3 (inr true))//. + + by apply: measurable_ysection. + + by apply/seteqP; split => z /=; rewrite /ysection /= inE. +Qed. + +#[export] +HB.instance Definition _ := isKernel.Build _ _ _ _ _ + (case_sum') (measurable_fun_case_sum'). +End kcase_sum'. + +Section sfkcase_sum'. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). +Let A : measurableType _ := unit. +Let B : measurableType _ := bool. +Variables (k : (A + B)%type -> R.-sfker X ~> Y). + +Let sfinite_case_sum' : exists2 k_ : (R.-ker _ ~> _)^nat, + forall n, measure_fam_uub (k_ n) & + forall x U, measurable U -> case_sum' k x U = mseries (k_ ^~ x) 0 U. +Proof. +rewrite /=. +set f : A + B -> (R.-fker _ ~> _)^nat := + fun ab : A + B => sval (cid (sfinite_kernel (k ab))). +set Hf := fun ab : A + B => svalP (cid (sfinite_kernel (k ab))). +rewrite /= in Hf. +exists (fun n => [the R.-ker _ ~> _ of case_sum' (fun ab => [the R.-fker _ ~> _ of f ab n])]). + move=> n /=. + have [rtt Hrtt] := measure_uub (f (inl tt) n). + have [rfalse Hrfalse] := measure_uub (f (inr false) n). + have [rtrue Hrtrue] := measure_uub (f (inr true) n). + exists (maxr rtt (maxr rfalse rtrue)) => //= -[x [[]|[|]]] /=. + by rewrite 2!EFin_max lt_maxr Hrtt. + by rewrite /CASE_SUM.case_sumr /= 2!EFin_max 2!lt_maxr Hrtrue 2!orbT. + by rewrite /CASE_SUM.case_sumr /= 2!EFin_max 2!lt_maxr Hrfalse orbT. +move=> [x [[]|[|]]] U mU/=-. +by rewrite (Hf (inl tt) x _ mU). +by rewrite (Hf (inr true) x _ mU). +by rewrite (Hf (inr false) x _ mU). +Qed. + +#[export] +HB.instance Definition _ := @Kernel_isSFinite_subdef.Build _ _ _ _ _ + (case_sum' k) (sfinite_case_sum'). +End sfkcase_sum'. + +End case_sum'. + +Section case_sum. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). +Let A : measurableType _ := unit. +Let B : measurableType _ := bool. + +Import CASE_SUM. + +(* case analysis on the datatype unit + bool *) +Definition case_sum (f : R.-sfker X ~> (A + B)%type) + (k : (A + B)%type-> R.-sfker X ~> Y) : R.-sfker X ~> Y := + [the R.-sfker X ~> (A + B)%type of f] \; + [the R.-sfker X * (A + B) ~> Y of case_sum' k]. + +End case_sum. + +(* counting measure as a kernel *) +Section kcounting. +Context d (G : measurableType d) (R : realType). + +Definition kcounting : G -> {measure set nat -> \bar R} := fun=> counting. + +Let mkcounting U : measurable U -> measurable_fun setT (kcounting ^~ U). +Proof. by []. Qed. + +HB.instance Definition _ := isKernel.Build _ _ _ _ _ kcounting mkcounting. + +Let sfkcounting : exists2 k_ : (R.-ker _ ~> _)^nat, + forall n, measure_fam_uub (k_ n) & + forall x U, measurable U -> kcounting x U = mseries (k_ ^~ x) 0 U. +Proof. +exists (fun n => [the R.-fker _ ~> _ of + @kdirac _ _ G nat R _ (@measurable_cst _ _ _ _ setT n)]). + by move=> n /=; exact: measure_uub. +by move=> g U mU; rewrite /kcounting/= counting_dirac. +Qed. + +HB.instance Definition _ := + Kernel_isSFinite_subdef.Build _ _ _ _ R kcounting sfkcounting. + +End kcounting. + +(* formalization of the iterate construct of Staton ESOP 2017, Sect. 4.2 *) +Section iterate. +Context d {G : measurableType d} {R : realType}. +Let A : measurableType _ := unit. +Let B : measurableType _ := bool. + +(* formalization of iterate^n + Gamma |-p iterate^n t from x = u : B *) +Variables (t : R.-sfker (G * A) ~> (A + B)%type) + (u : G -> A) (mu : measurable_fun setT u). + +Fixpoint iterate_ n : R.-sfker G ~> B := + match n with + | 0%N => case_sum (letin (ret mu) t) + (fun x => match x with + | inl a => fail + | inr b => ret (measurable_cst b) + end) + | m.+1 => case_sum (letin (ret mu) t) + (fun x => match x with + | inl a => iterate_ m + | inr b => fail + end) + end. + +(* formalization of iterate (A = unit, B = bool) + Gamma, x : A |-p t : A + B Gamma |-d u : A +----------------------------------------------- + Gamma |-p iterate t from x = u : B *) +Definition iterate : R.-sfker G ~> B := case_nat (kcounting R) iterate_. + +End iterate. + +(* an s-finite kernel to test that two expressions are different *) +Section lift_neq. +Context {R : realType} d (G : measurableType d). +Variables (f : G -> bool) (g : G -> bool). + +Definition flift_neq : G -> bool := fun x' => f x' != g x'. + +Hypotheses (mf : measurable_fun setT f) (mg : measurable_fun setT g). + +(* see also emeasurable_fun_neq *) +Lemma measurable_fun_flift_neq : measurable_fun setT flift_neq. +Proof. +apply: (measurable_fun_bool true). +rewrite /flift_neq /= (_ : _ @^-1` _ = ([set x | f x] `&` [set x | ~~ g x]) `|` + ([set x | ~~ f x] `&` [set x | g x])). + apply: measurableU; apply: measurableI. + - by rewrite -[X in measurable X]setTI; exact: mf. + - rewrite [X in measurable X](_ : _ = ~` [set x | g x]); last first. + by apply/seteqP; split => x /= /negP. + by apply: measurableC; rewrite -[X in measurable X]setTI; exact: mg. + - rewrite [X in measurable X](_ : _ = ~` [set x | f x]); last first. + by apply/seteqP; split => x /= /negP. + by apply: measurableC; rewrite -[X in measurable X]setTI; exact: mf. + - by rewrite -[X in measurable X]setTI; exact: mg. +by apply/seteqP; split => x /=; move: (f x) (g x) => [|] [|]//=; intuition. +Qed. + +Definition lift_neq : R.-sfker G ~> bool := ret measurable_fun_flift_neq. + +End lift_neq. + +Section von_neumann_trick. +Context d {T : measurableType d} {R : realType}. + +Definition kinrtt {d1 d2} {T1 : measurableType d1} {T2 : measurableType d2} := + @measurable_cst _ _ T1 _ setT (@inl unit T2 tt). + +Definition finlb d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) + : T1 * bool -> T2 + bool := fun t1b => inr t1b.2. + +Lemma minlb {d1 d2} {T1 : measurableType d1} {T2 : measurableType d2} : + measurable_fun setT (@finlb _ _ T1 T2). +Proof. exact: measurableT_comp. Qed. + +Variable (D : pprobability bool R (* biased coin *)). + +Definition von_neumann_trick' : R.-sfker (T * unit) ~> (unit + bool)%type := + letin (sample_cst D) + (letin (sample_cst D) + (letin (lift_neq macc1of3 macc2of3) + (ite (macc3of4) + (letin (ret macc1of4) (ret minlb)) + (ret kinrtt)))). + +Definition von_neumann_trick : R.-sfker T ~> bool := + iterate von_neumann_trick' ktt. + +End von_neumann_trick. diff --git a/theories/prob_lang_wip.v b/theories/prob_lang_wip.v index 7b12eed1b..9ab73014b 100644 --- a/theories/prob_lang_wip.v +++ b/theories/prob_lang_wip.v @@ -9,7 +9,7 @@ Require Import prob_lang. (******************************************************************************) (* Semantics of a probabilistic programming language using s-finite kernels *) -(* (wip) *) +(* (wip about definition of Lebesgue and counting measures) *) (******************************************************************************) Set Implicit Arguments. @@ -41,6 +41,9 @@ Qed. Definition gauss01_density : R -> R := gauss_density 0 1. +Hypothesis integral_gauss01_density : + (\int[lebesgue_measure]_x (gauss01_density x)%:E = 1%E)%E. + Lemma gauss01_densityE x : gauss01_density x = (sqrtr (pi *+ 2))^-1 * expR (- (x ^+ 2) / 2%:R). Proof. by rewrite /gauss01_density /gauss_density mul1r subr0 divr1. Qed. @@ -68,9 +71,6 @@ Proof. by rewrite /mgauss01 integral_ge0//= => x _; rewrite lee_fin gauss_density_ge0. Qed. -Axiom integral_gauss01_density : - (\int[lebesgue_measure]_x (gauss01_density x)%:E = 1%E)%E. - Let mgauss01_sigma_additive : semi_sigma_additive mgauss01. Proof. move=> /= F mF tF mUF. @@ -105,9 +105,15 @@ End gauss. Section gauss_lebesgue. Import Notations. Context d (T : measurableType d) (R : realType). +Hypothesis integral_gauss01_density : + (\int[@lebesgue_measure R]_x (gauss01_density x)%:E = 1%E)%E. Let f1 (x : R) := (gauss01_density x) ^-1. +Hypothesis integral_mgauss01 : forall U, measurable U -> + \int[mgauss01 (R:=R)]_(y in U) (f1 y)%:E = + \int[lebesgue_measure]_(x0 in U) (gauss01_density x0 * f1 x0)%:E. + Let mf1 : measurable_fun setT f1. Proof. apply: (measurable_comp (F := [set r : R | r != 0%R])) => //. @@ -121,10 +127,10 @@ Qed. Variable mu : {measure set mR R -> \bar R}. Definition staton_lebesgue : R.-sfker T ~> _ := - letin (sample (@gauss01 R)) + letin (sample_cst (gauss01 integral_gauss01_density : pprobability _ _)) (letin - (score (measurableT_comp mf1 var2of2)) - (ret var2of3)). + (score (measurableT_comp mf1 macc1of2)) + (ret macc1of3)). Lemma staton_lebesgueE x U : measurable U -> staton_lebesgue x U = lebesgue_measure U. @@ -133,16 +139,113 @@ move=> mU; rewrite [in LHS]/staton_lebesgue/=. rewrite [in LHS]letinE /=. transitivity (\int[@mgauss01 R]_(y in U) (f1 y)%:E). rewrite -[in RHS](setTI U) integral_setI_indic//=. - apply: eq_integral => /= r _. + apply: eq_integral => //= r. rewrite letinE/= ge0_integral_mscale//= ger0_norm//; last first. by rewrite invr_ge0// gauss_density_ge0. by rewrite integral_dirac// indicT mul1e diracE indicE. -transitivity (\int[lebesgue_measure]_(x in U) (gauss01_density x * f1 x)%:E). - admit. +rewrite integral_mgauss01//. transitivity (\int[lebesgue_measure]_(x in U) (\1_U x)%:E). apply: eq_integral => /= y yU. by rewrite /f1 divrr ?indicE ?yU// unitfE gt_eqF// gauss_density_gt0. by rewrite integral_indic//= setIid. -Abort. +Qed. End gauss_lebesgue. + +(* TODO: move this elsewhere *) +(* assuming x > 0 *) +Definition Gamma {R : realType} (x : R) : \bar R := + \int[lebesgue_measure]_(t in `[0%R, +oo[%classic) (expR (- t) * powR t (x - 1))%:E. + +Definition Rfact {R : realType} (x : R) := Gamma (x + 1)%R. + +Section poisson. +Variable R : realType. +Local Open Scope ring_scope. +Hypothesis integral_poisson_density : forall k, + (\int[lebesgue_measure]_x (@poisson R k x)%:E = 1%E)%E. + +(* density function for poisson *) +Definition poisson1 := @poisson R 1%N. + +Lemma poisson1_ge0 (x : R) : 0 <= poisson1 x. +Proof. exact: poisson_ge0. Qed. + +Definition mpoisson1 (V : set R) : \bar R := + (\int[lebesgue_measure]_(x in V) (poisson1 x)%:E)%E. + +Lemma measurable_fun_poisson1 : measurable_fun setT poisson1. +Proof. exact: measurable_poisson. Qed. + +Let mpoisson10 : mpoisson1 set0 = 0%E. +Proof. by rewrite /mpoisson1 integral_set0. Qed. + +Lemma mpoisson1_ge0 A : (0 <= mpoisson1 A)%E. +Proof. +apply: integral_ge0 => x Ax. +by rewrite lee_fin poisson1_ge0. +Qed. + +Let mpoisson1_sigma_additive : semi_sigma_additive mpoisson1. +Proof. +move=> /= F mF tF mUF. +rewrite /mpoisson1/= integral_bigcup//=; last first. + apply/integrableP; split. + apply/EFin_measurable_fun. + exact: measurable_funS (measurable_poisson _). + rewrite (_ : (fun x => _) = (EFin \o poisson1)); last first. + by apply/funext => x; rewrite gee0_abs// lee_fin poisson1_ge0//. + apply: le_lt_trans. + apply: (@subset_integral _ _ _ _ _ setT) => //=. + by apply/EFin_measurable_fun; exact: measurable_poisson. + by move=> ? _; rewrite lee_fin poisson1_ge0//. + by rewrite /= integral_poisson_density// ltry. +apply: is_cvg_ereal_nneg_natsum_cond => n _ _. +by apply: integral_ge0 => /= x ?; rewrite lee_fin poisson1_ge0. +Qed. + +HB.instance Definition _ := isMeasure.Build _ _ _ + mpoisson1 mpoisson10 mpoisson1_ge0 mpoisson1_sigma_additive. + +Let mpoisson1_setT : mpoisson1 [set: _] = 1%E. +Proof. +rewrite /mpoisson1. +rewrite /poisson1. +by rewrite integral_poisson_density. +Qed. + +HB.instance Definition _ := @Measure_isProbability.Build _ _ R mpoisson1 mpoisson1_setT. + +Definition poisson' := [the probability _ _ of mpoisson1]. + +End poisson. + +(* Staton's definition of the counting measure + Staton ESOP 2017, Sect. 4.2, equation (13) *) +Section staton_counting. +Context d (X : measurableType d). +Variable R : realType. +Import Notations. +Hypothesis integral_poisson_density : forall k, + (\int[lebesgue_measure]_x (@poisson R k x)%:E = 1%E)%E. + +Let f1 x := (poisson1 (x : R)) ^-1. + +Let mf1 : measurable_fun setT f1. +rewrite /f1 /poisson1 /poisson. +apply: (measurable_comp (F := [set r : R | r != 0%R])) => //. +- exact: open_measurable. +- move=> /= r [t ? <-]. + by case: ifPn => // t0; rewrite gt_eqF ?mulr_gt0 ?expR_gt0//= invrK ltr0n. +- apply: open_continuous_measurable_fun => //. + by apply/in_setP => x /= x0; exact: inv_continuous. +- exact: measurable_poisson. +Qed. + +Definition staton_counting : R.-sfker X ~> _ := + letin (sample_cst (@poisson' R integral_poisson_density : pprobability _ _)) + (letin + (score (measurableT_comp mf1 macc1of2)) + (ret macc1of3)). + +End staton_counting. From 47c3495271f0f7e0485898c61bbc099c39bab8d0 Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Sat, 16 Sep 2023 15:19:41 +0900 Subject: [PATCH 08/36] rm duplicate, more uniform naming --- theories/lang_syntax.v | 57 ++++++------ theories/lang_syntax_examples.v | 143 +++++++++++++++++++---------- theories/lang_syntax_toy.v | 8 +- theories/lang_syntax_util.v | 5 +- theories/prob_lang.v | 158 ++++++++++++++++---------------- theories/prob_lang_wip.v | 27 +++--- 6 files changed, 217 insertions(+), 181 deletions(-) diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index f2c305e7c..2da612f84 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -3,9 +3,10 @@ From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. From mathcomp.classical Require Import functions cardinality fsbigop. -Require Import signed reals ereal topology normedtype sequences esum measure. -Require Import lebesgue_measure numfun lebesgue_integral kernel prob_lang. -Require Import lang_syntax_util. +From mathcomp Require Import signed reals ereal topology normedtype sequences. +From mathcomp Require Import esum measure lebesgue_measure numfun. +From mathcomp Require Import lebesgue_integral kernel prob_lang. +From mathcomp Require Import lang_syntax_util. From mathcomp Require Import ring lra. (******************************************************************************) @@ -89,7 +90,7 @@ Proof. done. Qed. Let mswap_sigma_additive x : semi_sigma_additive (mswap x). Proof. exact: measure_semi_sigma_additive. Qed. -HB.instance Definition _ x := isMeasure.Build _ R _ +HB.instance Definition _ x := isMeasure.Build _ _ R (mswap x) (mswap0 x) (mswap_ge0 x) (@mswap_sigma_additive x). Definition mkswap : _ -> {measure set Z -> \bar R} := @@ -129,7 +130,7 @@ by rewrite /mswap/= kE. Qed. HB.instance Definition _ := - Kernel_isSFinite_subdef.Build _ _ _ Z R (mkswap k) mkswap_sfinite. + isSFiniteKernel_subdef.Build _ _ _ Z R (mkswap k) mkswap_sfinite. End mswap_sfinite_kernel. @@ -185,11 +186,11 @@ Let T0 z : (T' z) set0 = 0. Proof. by []. Qed. Let T_ge0 z x : 0 <= (T' z) x. Proof. by []. Qed. Let T_semi_sigma_additive z : semi_sigma_additive (T' z). Proof. exact: measure_semi_sigma_additive. Qed. -HB.instance Definition _ z := @isMeasure.Build _ R X (T' z) (T0 z) (T_ge0 z) +HB.instance Definition _ z := @isMeasure.Build _ X R (T' z) (T0 z) (T_ge0 z) (@T_semi_sigma_additive z). Let sfinT z : sfinite_measure (T' z). Proof. exact: sfinite_kernel_measure. Qed. -HB.instance Definition _ z := @Measure_isSFinite_subdef.Build _ X R +HB.instance Definition _ z := @isSFinite.Build _ X R (T' z) (sfinT z). Definition U' z : set Y -> \bar R := u z. @@ -197,11 +198,11 @@ Let U0 z : (U' z) set0 = 0. Proof. by []. Qed. Let U_ge0 z x : 0 <= (U' z) x. Proof. by []. Qed. Let U_semi_sigma_additive z : semi_sigma_additive (U' z). Proof. exact: measure_semi_sigma_additive. Qed. -HB.instance Definition _ z := @isMeasure.Build _ R Y (U' z) (U0 z) (U_ge0 z) +HB.instance Definition _ z := @isMeasure.Build _ Y R (U' z) (U0 z) (U_ge0 z) (@U_semi_sigma_additive z). Let sfinU z : sfinite_measure (U' z). Proof. exact: sfinite_kernel_measure. Qed. -HB.instance Definition _ z := @Measure_isSFinite_subdef.Build _ Y R +HB.instance Definition _ z := @isSFinite.Build _ Y R (U' z) (sfinU z). Lemma letin'C z A : measurable A -> @@ -220,7 +221,7 @@ under eq_integral. under eq_integral do rewrite retE /=. over. rewrite (sfinite_Fubini (T' z) (U' z) (fun x => \d_(x.1, x.2) A ))//; last first. - apply/EFin_measurable_fun => /=; rewrite (_ : (fun x => _) = mindic R mA)//. + apply/measurable_EFinP => /=; rewrite (_ : (fun x => _) = mindic R mA)//. by apply/funext => -[]. rewrite /=. apply: eq_integral => y _. @@ -271,7 +272,7 @@ Inductive typ := | Pair : typ -> typ -> typ | Prob : typ -> typ. -Canonical stype_eqType := Equality.Pack (@gen_eqMixin typ). +HB.instance Definition _ := gen_eqMixin typ. Fixpoint measurable_of_typ (t : typ) : {d & measurableType d} := match t with @@ -449,12 +450,6 @@ Notation "'Normalize' e" := (exp_normalize e) Notation "'if' e1 'then' e2 'else' e3" := (exp_if e1 e2 e3) (in custom expr at level 1) : lang_scope. -Local Open Scope lang_scope. -Example three_letin {R : realType} x y z : @exp R P [::] _ := - [let x := return {1}:R in - let y := return #x in - let z := return #y in return #z]. - Section free_vars. Context {R : realType}. @@ -548,7 +543,7 @@ by move=> z U mU; by rewrite /kweak/= hs. Qed. HB.instance Definition _ := - Kernel_isSFinite_subdef.Build _ _ _ _ _ (@kweak g h x t f) sf. + isSFiniteKernel_subdef.Build _ _ _ _ _ (@kweak g h x t f) sf. End sfkernel_weak. @@ -609,7 +604,7 @@ Inductive evalD : forall g t, exp D g t -> | eval_normalize g t (e : exp P g t) k : e -P> k -> - [Normalize e] -D> normalize k point ; measurable_fun_mnormalize k + [Normalize e] -D> normalize_pt k ; measurable_normalize_pt k | evalD_if g t e f mf (e1 : exp D g t) f1 mf1 e2 f2 mf2 : e -D> f ; mf -> e1 -D> f1 ; mf1 -> e2 -D> f2 ; mf2 -> @@ -630,8 +625,8 @@ with evalP : forall g t, exp P g t -> pval R g t -> Prop := [let str := e1 in e2] -P> letin' k1 k2 | eval_sample g t (e : exp _ _ (Prob t)) - (p : mctx g -> pprobability (mtyp t) R) mp : - e -D> p ; mp -> [Sample e] -P> sample p mp + (f : mctx g -> probability (mtyp t) R) mf : + e -D> f ; mf -> [Sample e] -P> sample f mf | eval_score g (e : exp _ g _) f mf : e -D> f ; mf -> [Score e] -P> kscore mf @@ -750,13 +745,13 @@ all: (rewrite {g t e u v mu mv hu}). inj_ex H6; subst e5. inj_ex H5; subst e4. by rewrite (IH1 _ H4) (IH2 _ H8). -- move=> g t e p mp ev IH k. +- move=> g t e f mf ev IH k. inversion 1; subst g0. inj_ex H5; subst t0. inj_ex H5; subst e1. inj_ex H7; subst k. - have ? := IH _ _ H3; subst p1. - by have -> : mp = mp1 by []. + have ? := IH _ _ H3; subst f1. + by have -> : mf = mf1 by []. - move=> g e f mf ev IH k. inversion 1; subst g0. inj_ex H0; subst e0. @@ -875,12 +870,12 @@ all: rewrite {g t e u v eu}. inj_ex H5; subst e4. inj_ex H6; subst e5. by rewrite (IH1 _ H4) (IH2 _ H8). -- move=> g t e p mp ep IH v. +- move=> g t e f mf ep IH v. inversion 1; subst g0 t0. inj_ex H7; subst v. inj_ex H5; subst e1. - have ? := IH _ _ H3; subst p1. - by have -> : mp = mp1 by []. + have ? := IH _ _ H3; subst f1. + by have -> : mf = mf1 by []. - move=> g e f mf ev IH k. inversion 1; subst g0. inj_ex H0; subst e0. @@ -931,7 +926,7 @@ all: rewrite {z g t}. - move=> g h e [f [mf H]]. by exists (poisson h \o f); eexists; exact: eval_poisson. - move=> g t e [k ek]. - by exists (normalize k point); eexists; exact: eval_normalize. + by exists (normalize_pt k); eexists; exact: eval_normalize. - move=> g t1 t2 x e1 [k1 ev1] e2 [k2 ev2]. by exists (letin' k1 k2); exact: eval_letin. - move=> g t e [f [/= mf ef]]. @@ -1072,10 +1067,10 @@ Lemma execD_bernoulli g r (r1 : (r%:num <= 1)%R) : existT _ (cst [the probability _ _ of bernoulli r1]) (measurable_cst _). Proof. exact/execD_evalD/eval_bernoulli. Qed. -Lemma execD_normalize g t (e : exp P g t) : +Lemma execD_normalize_pt g t (e : exp P g t) : @execD g _ [Normalize e] = - existT _ (normalize (execP e) point : _ -> pprobability _ _) - (measurable_fun_mnormalize (execP e)). + existT _ (normalize_pt (execP e) : _ -> pprobability _ _) + (measurable_normalize_pt (execP e)). Proof. exact/execD_evalD/eval_normalize/evalP_execP. Qed. Lemma execD_poisson g n (e : exp D g Real) : diff --git a/theories/lang_syntax_examples.v b/theories/lang_syntax_examples.v index 98880bfd8..c0881a54d 100644 --- a/theories/lang_syntax_examples.v +++ b/theories/lang_syntax_examples.v @@ -3,9 +3,10 @@ From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. From mathcomp.classical Require Import functions cardinality fsbigop. -Require Import signed reals ereal topology normedtype sequences esum measure. -Require Import lebesgue_measure numfun lebesgue_integral kernel prob_lang. -Require Import lang_syntax_util lang_syntax. +From mathcomp Require Import signed reals ereal topology normedtype sequences. +From mathcomp Require Import esum measure lebesgue_measure numfun. +From mathcomp Require Import lebesgue_integral kernel prob_lang. +From mathcomp Require Import lang_syntax_util lang_syntax. From mathcomp Require Import ring lra. (******************************************************************************) @@ -59,7 +60,7 @@ Lemma letin'_sample_bernoulli d d' (T : measurableType d) Proof. rewrite letin'E/=. rewrite ge0_integral_measure_sum// 2!big_ord_recl/= big_ord0 adde0/=. -by rewrite !ge0_integral_mscale//= !integral_dirac//= indicT 2!mul1e. +by rewrite !ge0_integral_mscale//= !integral_dirac//= !diracT 2!mul1e. Qed. Section letin'_return. @@ -81,7 +82,7 @@ Lemma letin'_retk (f : X -> Y) (mf : measurable_fun setT f) (k : R.-sfker Y * X ~> Z) x U : measurable U -> letin' (ret mf) k x U = k (f x, x) U. Proof. -move=> mU; rewrite letin'E retE integral_dirac ?indicT ?mul1e//. +move=> mU; rewrite letin'E retE integral_dirac ?diracT ?mul1e//. exact: (measurableT_comp (measurable_kernel k _ mU)). Qed. @@ -137,18 +138,19 @@ Qed. Local Close Scope lang_scope. (* simple tests to check bidirectional hints *) -Module tests_bidi. +Module bidi_tests. +Section bidi_tests. Local Open Scope lang_scope. Import Notations. Context (R : realType). -Definition v1 x : @exp R P [::] _ := [ +Definition bidi_test1 x : @exp R P [::] _ := [ let x := return {1}:R in return #x]. -Definition v2 (a b : string) +Definition bidi_test2 (a b : string) (a := "a") (b := "b") - (* (H : infer (b != a)) *) + (* (ba : infer (b != a)) *) : @exp R P [::] _ := [ let a := return {1}:R in let b := return {true}:B in @@ -156,16 +158,28 @@ Definition v2 (a b : string) let d := return {4}:R in *) return (#a, #b)]. -Definition v3 (a b c d : string) (H1 : infer (b != a)) (H2 : infer (c != a)) - (H3 : infer (c != b)) (H4 : infer (a != b)) (H5 : infer (a != c)) - (H6 : infer (b != c)) : @exp R P [::] _ := [ +Definition bidi_test3 (a b c d : string) + (ba : infer (b != a)) (ca : infer (c != a)) + (cb : infer (c != b)) (ab : infer (a != b)) + (ac : infer (a != c)) (bc : infer (b != c)) : @exp R P [::] _ := [ let a := return {1}:R in let b := return {2}:R in let c := return {3}:R in (* let d := return {4}:R in *) return (#b, #a)]. -End tests_bidi. +Definition bidi_test4 (a b c d : string) + (ba : infer (b != a)) (ca : infer (c != a)) + (cb : infer (c != b)) (ab : infer (a != b)) + (ac : infer (a != c)) (bc : infer (b != c)) : @exp R P [::] _ := [ + let a := return {1}:R in + let b := return {2}:R in + let c := return {3}:R in + (* let d := return {4}:R in *) + return {exp_poisson O [#c(*{exp_var c erefl}*)]}]. + +End bidi_tests. +End bidi_tests. Section trivial_example. Local Open Scope lang_scope. @@ -175,7 +189,8 @@ Context {R : realType}. Lemma exec_normalize_return g x r : projT1 (@execD _ g _ [Normalize return r:R]) x = \d_r :> probability _ R. Proof. -by rewrite execD_normalize execP_return execD_real/= normalize_kdirac. +rewrite execD_normalize_pt execP_return execD_real/=. +exact: normalize_kdirac. Qed. End trivial_example. @@ -205,10 +220,10 @@ Lemma exec_sample_pair0 (A : set (bool * bool)) : Proof. rewrite !execP_letin !execP_sample !execD_bernoulli execP_return /=. rewrite execD_pair !exp_var'E (execD_var_erefl "x") (execD_var_erefl "y") /=. -rewrite letin'E integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !indicE !in_setT/= !mul1e. -rewrite !letin'E !integral_measure_add//= !ge0_integral_mscale//= /onem. -by rewrite !integral_dirac//= !indicE !in_setT/= !mul1e !diracE. +rewrite letin'E ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !diracE !in_setT/= !mul1e. +rewrite !letin'E !ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. +by rewrite !integral_dirac//= !diracT !mul1e !diracE. Qed. Lemma exec_sample_pair0_TandT : @@ -235,7 +250,7 @@ Qed. Lemma exec_sample_pair_TorT : (projT1 (execD sample_pair_syntax)) tt [set p | p.1 || p.2] = (2 / 3)%:E. Proof. -rewrite execD_normalize normalizeE/= exec_sample_pair0. +rewrite execD_normalize_pt normalizeE/= exec_sample_pair0. do 4 rewrite mem_set//=. rewrite eqe ifF; last by apply/negbTE/negP => /orP[/eqP|//]; lra. rewrite exec_sample_pair0; do 3 rewrite mem_set//; rewrite memNset//=. @@ -259,7 +274,7 @@ Lemma exec_bernoulli13_score : execD bernoulli13_score = execD (exp_bernoulli (1 / 5%:R)%:nng (p1S 4)). Proof. apply: eq_execD. -rewrite execD_bernoulli/= /bernoulli13_score execD_normalize 2!execP_letin. +rewrite execD_bernoulli/= /bernoulli13_score execD_normalize_pt 2!execP_letin. rewrite execP_sample/= execD_bernoulli/= execP_if /= exp_var'E. rewrite (execD_var_erefl "x")/= !execP_return/= 2!execP_score 2!execD_real/=. apply: funext=> g; apply: eq_probability => U. @@ -269,28 +284,27 @@ under eq_integral. rewrite !letin'E. under eq_integral do rewrite retE /=. over. -rewrite !integral_measure_add //=; last by move=> b _; rewrite integral_ge0. +rewrite !ge0_integral_measure_add //=; last by move=> b _; rewrite integral_ge0. rewrite !ge0_integral_mscale //=; last 2 first. by move=> b _; rewrite integral_ge0. by move=> b _; rewrite integral_ge0. -rewrite !integral_dirac// !indicE !in_setT !mul1e. +rewrite !integral_dirac// !diracT !mul1e. rewrite iteE/= !ge0_integral_mscale//=. rewrite ger0_norm//. rewrite !integral_indic//= !iteE/= /mscale/=. -rewrite setTI diracE !in_setT !mule1. +rewrite setTI !diracT !mule1. rewrite ger0_norm//. rewrite -EFinD/= eqe ifF; last first. by apply/negbTE/negP => /orP[/eqP|//]; rewrite /onem; lra. rewrite !letin'E/= !iteE/=. rewrite !ge0_integral_mscale//=. rewrite ger0_norm//. -rewrite !integral_dirac//= !indicE !in_setT /= !mul1e ger0_norm//. +rewrite !integral_dirac//= !diracT /= !mul1e ger0_norm// !indicT. rewrite exp_var'E (execD_var_erefl "x")/=. rewrite /bernoulli/= measure_addE/= /mscale/= !mul1r. -rewrite muleDl//; congr (_ + _)%E; - rewrite -!EFinM; - congr (_%:E); - by rewrite indicE /onem; case: (_ \in _); field. +by rewrite muleDl//; congr (_ + _)%E; + rewrite -!EFinM; congr (_%:E); + rewrite !indicE /onem /=; case: (_ \in _); field. Qed. Definition bernoulli12_score := [Normalize @@ -302,7 +316,7 @@ Lemma exec_bernoulli12_score : execD bernoulli12_score = execD (exp_bernoulli (1 / 3%:R)%:nng (p1S 2)). Proof. apply: eq_execD. -rewrite execD_bernoulli/= /bernoulli12_score execD_normalize 2!execP_letin. +rewrite execD_bernoulli/= /bernoulli12_score execD_normalize_pt 2!execP_letin. rewrite execP_sample/= execD_bernoulli/= execP_if /= exp_var'E. rewrite (execD_var_erefl "x")/= !execP_return/= 2!execP_score 2!execD_real/=. apply: funext=> g; apply: eq_probability => U. @@ -312,15 +326,15 @@ under eq_integral. rewrite !letin'E. under eq_integral do rewrite retE /=. over. -rewrite !integral_measure_add //=; last by move=> b _; rewrite integral_ge0. +rewrite !ge0_integral_measure_add //=; last by move=> b _; rewrite integral_ge0. rewrite !ge0_integral_mscale //=; last 2 first. by move=> b _; rewrite integral_ge0. by move=> b _; rewrite integral_ge0. -rewrite !integral_dirac// !indicE !in_setT !mul1e. +rewrite !integral_dirac// !diracT !mul1e. rewrite iteE/= !ge0_integral_mscale//=. rewrite ger0_norm//. rewrite !integral_indic//= !iteE/= /mscale/=. -rewrite setTI diracE !in_setT !mule1. +rewrite setTI !diracT !mule1. rewrite ger0_norm//. rewrite -EFinD/= eqe ifF; last first. apply/negbTE/negP => /orP[/eqP|//]. @@ -328,7 +342,7 @@ rewrite -EFinD/= eqe ifF; last first. rewrite !letin'E/= !iteE/=. rewrite !ge0_integral_mscale//=. rewrite ger0_norm//. -rewrite !integral_dirac//= !indicE !in_setT /= !mul1e ger0_norm//. +rewrite !integral_dirac//= !diracT /= !mul1e ger0_norm// !indicT. rewrite exp_var'E (execD_var_erefl "x")/=. rewrite /bernoulli/= measure_addE/= /mscale/= !mul1r. rewrite muleDl//; congr (_ + _)%E; @@ -350,9 +364,9 @@ Lemma exec_bernoulli14_score : execD bernoulli14_score = execD (exp_bernoulli (5%:R / 11%:R)%:nng p511). Proof. apply: eq_execD. -rewrite execD_bernoulli/= execD_normalize 2!execP_letin. +rewrite execD_bernoulli/= execD_normalize_pt 2!execP_letin. rewrite execP_sample/= execD_bernoulli/= execP_if /= !exp_var'E. -rewrite !execP_return/= 2!execP_score 2!execD_real/=. +rewrite !execP_return/= 2!execP_score !execD_real/=. rewrite !(execD_var_erefl "x")/=. apply: funext=> g; apply: eq_probability => U. rewrite normalizeE !letin'E/=. @@ -361,15 +375,15 @@ under eq_integral. rewrite !letin'E. under eq_integral do rewrite retE /=. over. -rewrite !integral_measure_add //=; last by move=> b _; rewrite integral_ge0. +rewrite !ge0_integral_measure_add //=; last by move=> b _; rewrite integral_ge0. rewrite !ge0_integral_mscale //=; last 2 first. by move=> b _; rewrite integral_ge0. by move=> b _; rewrite integral_ge0. -rewrite !integral_dirac// !indicE !in_setT !mul1e. -rewrite iteE/= !ge0_integral_mscale//=. +rewrite !integral_dirac// !diracT !mul1e !iteE/=. +rewrite !ge0_integral_mscale//=. rewrite ger0_norm//. -rewrite !integral_indic//= !iteE/= /mscale/=. -rewrite setTI diracE !in_setT !mule1. +rewrite !integral_cst//= !diracT !(mule1,mul1e). +rewrite !indicT/= !mulr1. rewrite ger0_norm//. rewrite -EFinD/= eqe ifF; last first. apply/negbTE/negP => /orP[/eqP|//]. @@ -377,8 +391,8 @@ rewrite -EFinD/= eqe ifF; last first. rewrite !letin'E/= !iteE/=. rewrite !ge0_integral_mscale//=. rewrite ger0_norm//. -rewrite !integral_dirac//= !indicE !in_setT /= !mul1e ger0_norm//. -rewrite /bernoulli/= measure_addE/= /mscale/= !mul1r. +rewrite !integral_dirac//= !diracT !mul1e ger0_norm//. +rewrite /bernoulli/= measure_addE/= /mscale/=. rewrite muleDl//; congr (_ + _)%E; rewrite -!EFinM; congr (_%:E); @@ -409,8 +423,8 @@ Lemma score_fail' d (X : measurableType d) {R : realType} Proof. apply/eq_sfkernel => x U. rewrite letin'E/= /sample; unlock. -rewrite integral_measure_add//= ge0_integral_mscale//= ge0_integral_mscale//=. -rewrite integral_dirac//= integral_dirac//= !indicT/= !mul1e. +rewrite ge0_integral_measure_add//= ge0_integral_mscale//= ge0_integral_mscale//=. +rewrite integral_dirac//= integral_dirac//= !diracT/= !mul1e. by rewrite /mscale/= iteE//= iteE//= fail'E mule0 adde0 ger0_norm. Qed. @@ -506,12 +520,43 @@ Local Open Scope lang_scope. Import Notations. Context {R : realType}. +Section tests. + +Local Notation "$ str" := (@exp_var _ _ str%string _ erefl) + (in custom expr at level 1, format "$ str"). + +Definition staton_bus_syntax0_generic (x r u : string) + (rx : infer (r != x)) (Rx : infer (u != x)) + (ur : infer (u != r)) (xr : infer (x != r)) + (xu : infer (x != u)) (ru : infer (r != u)) : @exp R P [::] _ := + [let x := Sample {exp_bernoulli (2 / 7%:R)%:nng p27} in + let r := if #x then return {3}:R else return {10}:R in + let u := Score {exp_poisson 4 [#r]} in + return #x]. + +Fail Definition staton_bus_syntax0_generic' (x r u : string) + (rx : infer (r != x)) (Rx : infer (u != x)) + (ur : infer (u != r)) (xr : infer (x != r)) + (xu : infer (x != u)) (ru : infer (r != u)) : @exp R P [::] _ := + [let x := Sample {exp_bernoulli (2 / 7%:R)%:nng p27} in + let r := if $x then return {3}:R else return {10}:R in + let u := Score {exp_poisson 4 [$r]} in + return $x]. + +Fail Definition staton_bus_syntax0' : @exp R _ [::] _ := + [let "x" := Sample {exp_bernoulli (2 / 7%:R)%:nng p27} in + let "r" := if ${"x"} then return {3}:R else return {10}:R in + let "_" := Score {exp_poisson 4 [${"r"}]} in + return ${"x"}]. + Definition staton_bus_syntax0 : @exp R _ [::] _ := [let "x" := Sample {exp_bernoulli (2 / 7%:R)%:nng p27} in let "r" := if #{"x"} then return {3}:R else return {10}:R in let "_" := Score {exp_poisson 4 [#{"r"}]} in return #{"x"}]. +End tests. + Definition staton_bus_syntax := [Normalize {staton_bus_syntax0}]. Let sample_bern : R.-sfker munit ~> mbool := sample_cst (bernoulli p27). @@ -558,8 +603,8 @@ by rewrite exp_var'E (execD_var_erefl "x") /=; congr ret. Qed. Lemma exec_staton_bus : execD staton_bus_syntax = - existT _ (normalize kstaton_bus' point) (measurable_fun_mnormalize _). -Proof. by rewrite execD_normalize exec_staton_bus0'. Qed. + existT _ (normalize_pt kstaton_bus') (measurable_normalize_pt _). +Proof. by rewrite execD_normalize_pt exec_staton_bus0'. Qed. Let poisson4 := @poisson R 4%N. @@ -662,8 +707,8 @@ by rewrite exp_var'E (execD_var_erefl "x") /=; congr ret. Qed. Lemma exec_statonA_bus : execD staton_busA_syntax = - existT _ (normalize kstaton_busA' point) (measurable_fun_mnormalize _). -Proof. by rewrite execD_normalize exec_staton_busA0'. Qed. + existT _ (normalize_pt kstaton_busA') (measurable_normalize_pt _). +Proof. by rewrite execD_normalize_pt exec_staton_busA0'. Qed. (* equivalence between staton_bus and staton_busA *) Lemma staton_bus_staton_busA : @@ -709,8 +754,6 @@ Section letinC. Local Open Scope lang_scope. Variable (R : realType). -Require Import Classical_Prop. - Lemma letinC g t1 t2 (e1 : @exp R P g t1) (e2 : exp P g t2) (x y : string) (xy : infer (x != y)) (yx : infer (y != x)) diff --git a/theories/lang_syntax_toy.v b/theories/lang_syntax_toy.v index a328ea1cd..350a3d271 100644 --- a/theories/lang_syntax_toy.v +++ b/theories/lang_syntax_toy.v @@ -2,8 +2,8 @@ Require Import String Classical. From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg. From mathcomp.classical Require Import mathcomp_extra boolp. -Require Import signed reals topology normedtype. -Require Import lang_syntax_util. +From mathcomp Require Import signed reals topology normedtype. +From mathcomp Require Import lang_syntax_util. (******************************************************************************) (* Intrinsically-typed concrete syntax for a toy language *) @@ -36,7 +36,7 @@ Variables (R : realType). Inductive typ := Real | Unit. -Canonical typ_eqType := Equality.Pack (@gen_eqMixin typ). +HB.instance Definition _ := gen_eqMixin typ. Definition iter_pair (l : list Type) : Type := foldr (fun x y => (x * y)%type) unit l. @@ -169,7 +169,7 @@ Implicit Types str : string. Inductive typ := Real | Unit | Pair : typ -> typ -> typ. -Canonical typ_eqType := Equality.Pack (@gen_eqMixin typ). +HB.instance Definition _ := gen_eqMixin typ. Fixpoint mtyp (t : typ) : Type := match t with diff --git a/theories/lang_syntax_util.v b/theories/lang_syntax_util.v index ad84918c9..4e029ae27 100644 --- a/theories/lang_syntax_util.v +++ b/theories/lang_syntax_util.v @@ -2,14 +2,13 @@ Require Import String. From HB Require Import structures. Require Import Classical_Prop. (* NB: to compile with Coq 8.17 *) From mathcomp Require Import all_ssreflect. -Require Import signed. +From mathcomp Require Import signed. (******************************************************************************) (* Shared by lang_syntax_*.v files *) (******************************************************************************) -Definition string_eqMixin := @EqMixin string String.eqb eqb_spec. -Canonical string_eqType := EqType string string_eqMixin. +HB.instance Definition _ := hasDecEq.Build string eqb_spec. Ltac inj_ex H := revert H; match goal with diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 48e78ef85..145994e05 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -1,11 +1,13 @@ (* mathcomp analysis (c) 2022 Inria and AIST. License: CeCILL-C. *) From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval finmap. -From mathcomp Require Import rat. +From mathcomp Require Import rat archimedean. From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. From mathcomp.classical Require Import functions cardinality fsbigop. -Require Import reals ereal signed topology normedtype sequences esum measure. -Require Import lebesgue_measure numfun lebesgue_integral exp kernel. +From mathcomp Require Import reals ereal signed topology normedtype sequences. +From mathcomp Require Import esum measure lebesgue_measure numfun. +From mathcomp Require Import lebesgue_integral exp kernel. +From mathcomp Require Import lra. (******************************************************************************) (* Semantics of a probabilistic programming language using s-finite kernels *) @@ -13,7 +15,8 @@ Require Import lebesgue_measure numfun lebesgue_integral exp kernel. (* bernoulli r1 == Bernoulli probability with r1 a proof that *) (* r : {nonneg R} is smaller than 1 *) (* uniform_probability a b ab0 == uniform probability over the interval [a,b] *) -(* sample_cst P == sample according to the probability P *) +(* sample mP == sample according to the probability P where mP is a *) +(* proof that P is a measurable function *) (* letin l k == execute l, augment the context, and execute k *) (* ret mf == access the context with f and return the result *) (* score mf == observe t from d, where f is the density of d and *) @@ -115,7 +118,7 @@ Lemma integral_bernoulli {R : realType} Proof. move=> f0. rewrite ge0_integral_measure_sum// 2!big_ord_recl/= big_ord0 adde0/=. -by rewrite !ge0_integral_mscale//= !integral_dirac//= indicT 2!mul1e. +by rewrite !ge0_integral_mscale//= !integral_dirac//= !diracT 2!mul1e. Qed. Section uniform_probability. @@ -130,7 +133,7 @@ HB.instance Definition _ := Measure.on uniform_probability. Let uniform_probability_setT : uniform_probability [set: _] = 1. Proof. rewrite /uniform_probability /mscale/= /mrestr/=. -rewrite setTI lebesgue_measure_itv hlength_itv/= lte_fin. +rewrite setTI lebesgue_measure_itv/= lte_fin. by rewrite -subr_gt0 ab0 -EFinD -EFinM mulVf// gt_eqF// subr_gt0. Qed. @@ -228,7 +231,7 @@ rewrite (_ : (fun x => _) = (fun x => x * (\1_(`[i%:R%:E, i.+1%:R%:E [%classic : set _) x)%:E)); last first. apply/funext => x; case: ifPn => ix; first by rewrite indicE/= mem_set ?mule1. by rewrite indicE/= memNset ?mule0// /= in_itv/=; exact/negP. -apply: emeasurable_funM => //=; apply/EFin_measurable_fun. +apply: emeasurable_funM => //=; apply/measurable_EFinP. by rewrite (_ : \1__ = mindic R (emeasurable_itv `[(i%:R)%:E, (i.+1%:R)%:E[)). Qed. @@ -284,14 +287,14 @@ pose floor_f := widen_ord (leq_addl n `|floor `|f t| |.+1) (Ordinal (ltnSn `|floor `|f t| |)). rewrite big_mkord (bigD1 floor_f)//= ifT; last first. rewrite lee_fin lte_fin; apply/andP; split. - by rewrite natr_absz (@ger0_norm _ (floor `|f t|)) ?floor_ge0 ?floor_le. + by rewrite natr_absz (@ger0_norm _ (floor `|f t|)) ?floor_ge0 ?ge_floor. rewrite -addn1 natrD natr_absz. - by rewrite (@ger0_norm _ (floor `|f t|)) ?floor_ge0 ?lt_succ_floor. + by rewrite (@ger0_norm _ (floor `|f t|)) ?floor_ge0// intrD1 lt_succ_floor. rewrite big1 ?adde0//= => j jk. rewrite ifF// lte_fin lee_fin. move: jk; rewrite neq_ltn/= => /orP[|] jr. - suff : (j.+1%:R <= `|f t|)%R by rewrite leNgt => /negbTE ->; rewrite andbF. - rewrite (_ : j.+1%:R = j.+1%:~R)// floor_ge_int. + rewrite (_ : j.+1%:R = j.+1%:~R)// floor_ge_int//. move: jr; rewrite -lez_nat => /le_trans; apply. by rewrite -[leRHS](@ger0_norm _ (floor `|f t|)) ?floor_ge0. - suff : (`|f t| < j%:R)%R by rewrite ltNge => /negbTE ->. @@ -346,7 +349,7 @@ by rewrite /mseries eseries0. Qed. #[export] -HB.instance Definition _ := @Kernel_isSFinite_subdef.Build _ _ _ _ _ +HB.instance Definition _ := @isSFiniteKernel_subdef.Build _ _ _ _ _ (kiteT k) sfinite_kiteT. End sfkiteT. @@ -403,7 +406,7 @@ by rewrite /mseries eseries0. Qed. #[export] -HB.instance Definition _ := @Kernel_isSFinite_subdef.Build _ _ _ _ _ +HB.instance Definition _ := @isSFiniteKernel_subdef.Build _ _ _ _ _ (kiteF k) sfinite_kiteF. End sfkiteF. @@ -469,14 +472,28 @@ Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). Definition ret (f : X -> Y) (mf : measurable_fun setT f) : R.-pker X ~> Y := [the R.-pker _ ~> _ of kdirac mf]. +Definition sample (P : X -> pprobability Y R) (mP : measurable_fun setT P) + : R.-pker X ~> Y := + [the R.-pker _ ~> _ of kprobability mP]. + Definition sample_cst (P : pprobability Y R) : R.-pker X ~> Y := - [the R.-pker _ ~> _ of kprobability (measurable_cst P)]. + sample (measurable_cst P). -Definition sample (P : X -> pprobability Y R) (mP : measurable_fun setT P) : R.-pker X ~> Y := - [the R.-pker _ ~> _ of kprobability mP]. +Definition normalize (k : R.-ker X ~> Y) P : X -> probability Y R := + knormalize k P. -Definition normalize (k : R.-sfker X ~> Y) P : X -> probability Y R := - fun x => [the probability _ _ of mnormalize (k x) P]. +Definition normalize_pt (k : R.-ker X ~> Y) : X -> probability Y R := + normalize k point. + +Lemma measurable_normalize_pt (f : R.-ker X ~> Y) : + measurable_fun [set: X] (normalize_pt f : X -> pprobability Y R). +Proof. +apply: (@measurability _ _ _ _ _ _ + (@pset _ _ _ : set (set (pprobability Y R)))) => //. +move=> _ -[_ [r r01] [Ys mYs <-]] <-. +apply: emeasurable_fun_infty_o => //. +exact: (measurable_kernel (knormalize f point) Ys). +Qed. Definition ite (f : X -> bool) (mf : measurable_fun setT f) (k1 k2 : R.-sfker X ~> Y) : R.-sfker X ~> Y := @@ -513,7 +530,7 @@ Proof. apply/eq_measure/funext => U. rewrite /ite; unlock => /=. rewrite /kcomp/= integral_dirac//=. -rewrite indicT mul1e. +rewrite diracT mul1e. rewrite -/(measure_add (ITE.kiteT k1 (x, f x)) (ITE.kiteF k2 (x, f x))). rewrite measure_addE. rewrite /ITE.kiteT /ITE.kiteF/=. @@ -567,13 +584,12 @@ rewrite integral_indic ?setIT// -[X in measurable X]setTI. exact: (measurableT_comp mf). Qed. -Lemma letin_retk - (f : X -> Y) (mf : measurable_fun setT f) - (k : R.-sfker [the measurableType _ of (X * Y)%type] ~> Z) - x U : measurable U -> +Lemma letin_retk (f : X -> Y) + (mf : measurable_fun [set: X] f) (k : R.-sfker X * Y ~> Z) x U : + measurable U -> letin (ret mf) k x U = k (x, f x) U. Proof. -move=> mU; rewrite letinE retE integral_dirac ?indicT ?mul1e//. +move=> mU; rewrite letinE retE integral_dirac ?diracT ?mul1e//. exact: (measurableT_comp (measurable_kernel k _ mU)). Qed. @@ -878,7 +894,7 @@ Let kcomp_scoreE d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) (score mf \; g) r U = `|f r|%:E * g (r, tt) U. Proof. rewrite /= /kcomp /kscore /= ge0_integral_mscale//=. -by rewrite integral_dirac// indicT mul1e. +by rewrite integral_dirac// diracT mul1e. Qed. Lemma scoreE d' (T' : measurableType d') (x : T * T') (U : set T') (f : R -> R) @@ -910,8 +926,8 @@ Lemma score_fail (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : Proof. apply/eq_sfkernel => x U. rewrite letinE/= /sample; unlock. -rewrite integral_measure_add//= ge0_integral_mscale//= ge0_integral_mscale//=. -rewrite integral_dirac//= integral_dirac//= !indicT/= !mul1e. +rewrite ge0_integral_measure_add// ge0_integral_mscale//= ge0_integral_mscale//=. +rewrite integral_dirac//= integral_dirac//= !diracT/= !mul1e. by rewrite /mscale/= iteE//= iteE//= failE mule0 adde0 ger0_norm. Qed. @@ -989,23 +1005,22 @@ Let T0 z : (T z) set0 = 0. Proof. by []. Qed. Let T_ge0 z x : 0 <= (T z) x. Proof. by []. Qed. Let T_semi_sigma_additive z : semi_sigma_additive (T z). Proof. exact: measure_semi_sigma_additive. Qed. -HB.instance Definition _ z := @isMeasure.Build _ R X (T z) (T0 z) (T_ge0 z) +HB.instance Definition _ z := @isMeasure.Build _ X R (T z) (T0 z) (T_ge0 z) (@T_semi_sigma_additive z). Let sfinT z : sfinite_measure (T z). Proof. exact: sfinite_kernel_measure. Qed. -HB.instance Definition _ z := @Measure_isSFinite_subdef.Build _ X R - (T z) (sfinT z). +HB.instance Definition _ z := @isSFinite.Build _ X R (T z) (sfinT z). Definition U z : set Y -> \bar R := u z. Let U0 z : (U z) set0 = 0. Proof. by []. Qed. Let U_ge0 z x : 0 <= (U z) x. Proof. by []. Qed. Let U_semi_sigma_additive z : semi_sigma_additive (U z). Proof. exact: measure_semi_sigma_additive. Qed. -HB.instance Definition _ z := @isMeasure.Build _ R Y (U z) (U0 z) (U_ge0 z) +HB.instance Definition _ z := @isMeasure.Build _ Y R (U z) (U0 z) (U_ge0 z) (@U_semi_sigma_additive z). Let sfinU z : sfinite_measure (U z). Proof. exact: sfinite_kernel_measure. Qed. -HB.instance Definition _ z := @Measure_isSFinite_subdef.Build _ Y R +HB.instance Definition _ z := @isSFinite.Build _ Y R (U z) (sfinU z). Lemma letinC z A : measurable A -> @@ -1027,7 +1042,7 @@ rewrite (sfinite_Fubini [the {sfinite_measure set X -> \bar R} of T z] [the {sfinite_measure set Y -> \bar R} of U z] (fun x => \d_(x.1, x.2) A ))//; last first. - apply/EFin_measurable_fun => /=; rewrite (_ : (fun x => _) = mindic R mA)//. + apply/measurable_EFinP => /=; rewrite (_ : (fun x => _) = mindic R mA)//. by apply/funext => -[]. rewrite /=. apply: eq_integral => y _. @@ -1048,7 +1063,7 @@ by rewrite /onem/= -{1}(@divrr _ n.+1%:R) ?unitfE// -mulrBl -natr1 addrK. Qed. Lemma p1S n : (1 / n.+1%:R)%:nng%:num <= 1 :> R. -Proof. by rewrite ler_pdivr_mulr//= mul1r ler1n. Qed. +Proof. by rewrite ler_pdivrMr//= mul1r ler1n. Qed. Lemma p12 : (1 / 2%:R)%:nng%:num <= 1 :> R. Proof. by rewrite p1S. Qed. @@ -1058,7 +1073,7 @@ Lemma onem27 : `1- (2 / 7%:R) = (5%:R / 7%:R)%:nng%:num :> R. Proof. by apply/eqP; rewrite subr_eq/= -mulrDl -natrD divrr// unitfE. Qed. Lemma p27 : (2 / 7%:R)%:nng%:num <= 1 :> R. -Proof. by rewrite /= lter_pdivr_mulr// mul1r ler_nat. Qed. +Proof. by rewrite /= lter_pdivrMr// mul1r ler_nat. Qed. End constants. Arguments p12 {R}. @@ -1089,9 +1104,7 @@ Qed. Lemma measurable_poisson k : measurable_fun setT (poisson k). Proof. rewrite /poisson; apply: measurable_fun_if => //. - apply: (measurable_fun_bool true). - rewrite (_ : _ @^-1` _ = `]0, +oo[%classic)//. - by apply/seteqP; split => x /=; rewrite in_itv/= andbT. + exact: measurable_fun_ltr. by apply: measurable_funM => /=; [exact: measurable_funM|exact: measurableT_comp]. Qed. @@ -1130,7 +1143,7 @@ Lemma letin_sample_bernoulli d d' (T : measurableType d) Proof. rewrite letinE/=. rewrite ge0_integral_measure_sum// 2!big_ord_recl/= big_ord0 adde0/=. -by rewrite !ge0_integral_mscale//= !integral_dirac//= indicT 2!mul1e. +by rewrite !ge0_integral_mscale//= !integral_dirac//= !diracT 2!mul1e. Qed. Section sample_and_return. @@ -1168,7 +1181,7 @@ Lemma sample_and_branchE t U : sample_and_branch t U = (2 / 7%:R)%:E * \d_(3%:R : R) U + (5%:R / 7%:R)%:E * \d_(10%:R : R) U. Proof. -by rewrite /sample_and_branch letin_sample_bernoulli/= !iteE !retE onem27. +by rewrite /sample_and_branch letin_sample_bernoulli//= !iteE/= onem27. Qed. End sample_and_branch. @@ -1177,32 +1190,16 @@ Section bernoulli_and. Context d (T : measurableType d) (R : realType). Import Notations. -Definition mand (x y : T * mbool * mbool -> mbool) - (t : T * mbool * mbool) : mbool := x t && y t. - -Lemma measurable_fun_mand (x y : T * mbool * mbool -> mbool) : - measurable_fun setT x -> measurable_fun setT y -> - measurable_fun setT (mand x y). -Proof. -move=> /= mx my; apply: (measurable_fun_bool true). -rewrite [X in measurable X](_ : _ = - (x @^-1` [set true]) `&` (y @^-1` [set true])); last first. - by rewrite /mand; apply/seteqP; split => z/= /andP. -apply: measurableI. -- by rewrite -[X in measurable X]setTI; exact: mx. -- by rewrite -[X in measurable X]setTI; exact: my. -Qed. - Definition bernoulli_and : R.-sfker T ~> mbool := (letin (sample_cst [the probability _ _ of bernoulli p12]) (letin (sample_cst [the probability _ _ of bernoulli p12]) - (ret (measurable_fun_mand macc1of3 macc2of3)))). + (ret (measurable_and macc1of3 macc2of3)))). Lemma bernoulli_andE t U : bernoulli_and t U = sample_cst (bernoulli p14) t U. Proof. -rewrite /bernoulli_and 3!letin_sample_bernoulli/= /mand/= muleDr//= -muleDl//. +rewrite /bernoulli_and 3!letin_sample_bernoulli/= muleDr//= -muleDl//. rewrite !muleA -addeA -muleDl// -!EFinM !onem1S/= -splitr mulr1. have -> : (1 / 2 * (1 / 2) = 1 / 4%:R :> R)%R by rewrite mulf_div mulr1// -natrM. rewrite /bernoulli/= measure_addE/= /mscale/= -!EFinM; congr( _ + (_ * _)%:E). @@ -1328,8 +1325,8 @@ Qed. End staton_bus_exponential. (* X + Y is a measurableType if X and Y are *) -Canonical sum_pointedType (X Y : pointedType) := - PointedType (X + Y) (@inl X Y point). +HB.instance Definition _ (X Y : pointedType) := + isPointed.Build (X + Y)%type (@inl X Y point). Section measurable_sum. Context d d' (X : measurableType d) (Y : measurableType d'). @@ -1345,7 +1342,7 @@ Let sumU (F : (set (X + Y))^nat) : (forall i, measurable_sum (F i)) -> Proof. by []. Qed. HB.instance Definition _ := @isMeasurable.Build default_measure_display (X + Y)%type - (Pointed.class _) measurable_sum sum0 sumC sumU. + measurable_sum sum0 sumC sumU. End measurable_sum. @@ -1419,7 +1416,7 @@ by rewrite /mseries eseries0. Qed. #[export] -HB.instance Definition _ j := @Kernel_isSFinite_subdef.Build _ _ _ _ _ +HB.instance Definition _ j := @isSFiniteKernel_subdef.Build _ _ _ _ _ (case_nat_ k j) (sfcase_nat_ j). End sfcase_nat. @@ -1465,8 +1462,8 @@ Proof. exact. Qed. Definition image_classes d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) (T : Type) (f1 : T1 -> T) (f2 : T2 -> T) := - <>. + <>. Section sum_salgebra_instance. Context d1 d2 (T1 : measurableType d1) (T2 : measurableType d2). @@ -1486,8 +1483,8 @@ Proof. exact: sigma_algebra_bigcup. Qed. HB.instance Definition sum_salgebra_mixin := @isMeasurable.Build (measure_sum_display (d1, d2)) - (T1 + T2)%type (Pointed.class _) (image_classes f1 f2) - (sum_salgebra_set0) (sum_salgebra_setC) (sum_salgebra_bigcup). + (T1 + T2)%type (image_classes f1 f2) + sum_salgebra_set0 sum_salgebra_setC sum_salgebra_bigcup. End sum_salgebra_instance. Reserved Notation "p .-sum" (at level 1, format "p .-sum"). @@ -1540,7 +1537,7 @@ by rewrite /mseries hk. Qed. #[export] -HB.instance Definition _ (a : A) := @Kernel_isSFinite_subdef.Build _ _ _ _ _ +HB.instance Definition _ (a : A) := @isSFiniteKernel_subdef.Build _ _ _ _ _ (case_suml k a) (sfinite_case_suml a). End sfkcase_suml. @@ -1610,7 +1607,7 @@ by rewrite /mseries/= eseries0. Qed. #[export] -HB.instance Definition _ b := @Kernel_isSFinite_subdef.Build _ _ _ _ _ +HB.instance Definition _ b := @isSFiniteKernel_subdef.Build _ _ _ _ _ (case_sumr k b) (sfinite_case_sumr b). End sfkcase_sumr. @@ -1685,7 +1682,7 @@ have mh1 : measurable h1. move=> _ /= C mC; rewrite setTI. have := H measurableT _ mC; rewrite setTI => {}H. rewrite [X in measurable X](_ : _ = ((fun x => k (inl tt) x U) @^-1` C) `*` setT)//. - exact: measurableM. + exact: measurableX. by apply/seteqP; split => [z//=| z/= []]. set h2 := [set xub : X * (unit + bool)| k (inr false) xub.1 U < x%:E]. have mh2 : measurable h2. @@ -1695,7 +1692,7 @@ have mh2 : measurable h2. move=> _ /= C mC; rewrite setTI. have := H measurableT _ mC; rewrite setTI => {}H. rewrite [X in measurable X](_ : _ = ((fun x => k (inr false) x U) @^-1` C) `*` setT)//. - exact: measurableM. + exact: measurableX. by apply/seteqP; split => [z //=|z/= []]. set h3 := [set xub : X * (unit + bool)| k (inr true) xub.1 U < x%:E]. have mh3 : measurable h3. @@ -1705,19 +1702,19 @@ have mh3 : measurable h3. move=> _ /= C mC; rewrite setTI. have := H measurableT _ mC; rewrite setTI => {}H. rewrite [X in measurable X](_ : _ = ((fun x => k (inr true) x U) @^-1` C) `*` setT)//. - exact: measurableM. + exact: measurableX. by apply/seteqP; split=> [z//=|z/= []]. apply: measurableU. - apply: measurableU. - + apply: measurableM => //. + + apply: measurableX => //. rewrite [X in measurable X](_ : _ = ysection h1 (inl tt))//. * by apply: measurable_ysection. * by apply/seteqP; split => z /=; rewrite /ysection /= inE. - + apply: measurableM => //. + + apply: measurableX => //. rewrite [X in measurable X](_ : _ = ysection h2 (inr false))//. * by apply: measurable_ysection. * by apply/seteqP; split => z /=; rewrite /ysection /= inE. -- apply: measurableM => //. +- apply: measurableX => //. rewrite [X in measurable X](_ : _ = ysection h3 (inr true))//. + by apply: measurable_ysection. + by apply/seteqP; split => z /=; rewrite /ysection /= inE. @@ -1749,9 +1746,9 @@ exists (fun n => [the R.-ker _ ~> _ of case_sum' (fun ab => [the R.-fker _ ~> _ have [rfalse Hrfalse] := measure_uub (f (inr false) n). have [rtrue Hrtrue] := measure_uub (f (inr true) n). exists (maxr rtt (maxr rfalse rtrue)) => //= -[x [[]|[|]]] /=. - by rewrite 2!EFin_max lt_maxr Hrtt. - by rewrite /CASE_SUM.case_sumr /= 2!EFin_max 2!lt_maxr Hrtrue 2!orbT. - by rewrite /CASE_SUM.case_sumr /= 2!EFin_max 2!lt_maxr Hrfalse orbT. + by rewrite 2!EFin_max lt_max Hrtt. + by rewrite /CASE_SUM.case_sumr /= 2!EFin_max 2!lt_max Hrtrue 2!orbT. + by rewrite /CASE_SUM.case_sumr /= 2!EFin_max 2!lt_max Hrfalse orbT. move=> [x [[]|[|]]] U mU/=-. by rewrite (Hf (inl tt) x _ mU). by rewrite (Hf (inr true) x _ mU). @@ -1759,8 +1756,8 @@ by rewrite (Hf (inr false) x _ mU). Qed. #[export] -HB.instance Definition _ := @Kernel_isSFinite_subdef.Build _ _ _ _ _ - (case_sum' k) (sfinite_case_sum'). +HB.instance Definition _ := @isSFiniteKernel_subdef.Build _ _ _ _ _ + (case_sum' k) sfinite_case_sum'. End sfkcase_sum'. End case_sum'. @@ -1802,7 +1799,7 @@ by move=> g U mU; rewrite /kcounting/= counting_dirac. Qed. HB.instance Definition _ := - Kernel_isSFinite_subdef.Build _ _ _ _ R kcounting sfkcounting. + isSFiniteKernel_subdef.Build _ _ _ _ R kcounting sfkcounting. End kcounting. @@ -1851,7 +1848,8 @@ Hypotheses (mf : measurable_fun setT f) (mg : measurable_fun setT g). (* see also emeasurable_fun_neq *) Lemma measurable_fun_flift_neq : measurable_fun setT flift_neq. Proof. -apply: (measurable_fun_bool true). +apply: (@measurable_fun_bool _ _ _ _ true). +rewrite setTI. rewrite /flift_neq /= (_ : _ @^-1` _ = ([set x | f x] `&` [set x | ~~ g x]) `|` ([set x | ~~ f x] `&` [set x | g x])). apply: measurableU; apply: measurableI. diff --git a/theories/prob_lang_wip.v b/theories/prob_lang_wip.v index 9ab73014b..8c6c5a33e 100644 --- a/theories/prob_lang_wip.v +++ b/theories/prob_lang_wip.v @@ -3,9 +3,9 @@ From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval finmap. From mathcomp Require Import rat. From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. From mathcomp.classical Require Import functions cardinality fsbigop. -Require Import signed reals ereal topology normedtype sequences esum measure. -Require Import lebesgue_measure numfun lebesgue_integral exp kernel trigo. -Require Import prob_lang. +From mathcomp Require Import signed reals ereal topology normedtype sequences. +From mathcomp Require Import esum measure lebesgue_measure numfun. +From mathcomp Require Import lebesgue_integral exp kernel trigo prob_lang. (******************************************************************************) (* Semantics of a probabilistic programming language using s-finite kernels *) @@ -58,7 +58,7 @@ apply: measurable_funM => //=. apply: measurableT_comp => //=. apply: measurable_funM => //=. apply: measurableT_comp => //=. -apply: measurableT_comp (measurable_exprn _) _ => /=. +apply: measurableT_comp (exprn_measurable _) _ => /=. apply: measurable_funM => //=. exact: measurable_funD. Qed. @@ -76,13 +76,13 @@ Proof. move=> /= F mF tF mUF. rewrite /mgauss01/= integral_bigcup//=; last first. apply/integrableP; split. - apply/EFin_measurable_fun. + apply/measurable_EFinP. exact: measurable_funS (measurable_fun_gauss_density 0 1). rewrite (_ : (fun x => _) = (EFin \o gauss01_density)); last first. by apply/funext => x; rewrite gee0_abs// lee_fin gauss_density_ge0. apply: le_lt_trans. - apply: (@subset_integral _ _ _ _ _ setT) => //=. - apply/EFin_measurable_fun. + apply: (@ge0_subset_integral _ _ _ _ _ setT) => //=. + apply/measurable_EFinP. exact: measurable_fun_gauss_density. by move=> ? _; rewrite lee_fin gauss_density_ge0. by rewrite integral_gauss01_density// ltey. @@ -138,11 +138,12 @@ Proof. move=> mU; rewrite [in LHS]/staton_lebesgue/=. rewrite [in LHS]letinE /=. transitivity (\int[@mgauss01 R]_(y in U) (f1 y)%:E). - rewrite -[in RHS](setTI U) integral_setI_indic//=. - apply: eq_integral => //= r. + rewrite -[in RHS](setTI U) integral_mkcondr/=. + apply: eq_integral => //= r _. rewrite letinE/= ge0_integral_mscale//= ger0_norm//; last first. by rewrite invr_ge0// gauss_density_ge0. - by rewrite integral_dirac// indicT mul1e diracE indicE. + rewrite integral_dirac// diracT mul1e/= diracE epatch_indic/=. + by rewrite indicE. rewrite integral_mgauss01//. transitivity (\int[lebesgue_measure]_(x in U) (\1_U x)%:E). apply: eq_integral => /= y yU. @@ -191,13 +192,13 @@ Proof. move=> /= F mF tF mUF. rewrite /mpoisson1/= integral_bigcup//=; last first. apply/integrableP; split. - apply/EFin_measurable_fun. + apply/measurable_EFinP. exact: measurable_funS (measurable_poisson _). rewrite (_ : (fun x => _) = (EFin \o poisson1)); last first. by apply/funext => x; rewrite gee0_abs// lee_fin poisson1_ge0//. apply: le_lt_trans. - apply: (@subset_integral _ _ _ _ _ setT) => //=. - by apply/EFin_measurable_fun; exact: measurable_poisson. + apply: (@ge0_subset_integral _ _ _ _ _ setT) => //=. + by apply/measurable_EFinP; exact: measurable_poisson. by move=> ? _; rewrite lee_fin poisson1_ge0//. by rewrite /= integral_poisson_density// ltry. apply: is_cvg_ereal_nneg_natsum_cond => n _ _. From a6375fdcef05964d567a82f089ecf175314e5c9c Mon Sep 17 00:00:00 2001 From: AyumuSaito Date: Sat, 23 Sep 2023 11:15:51 +0900 Subject: [PATCH 09/36] add binary operations --- theories/lang_syntax.v | 81 +++++++++++++++++++++++++++++++++ theories/lang_syntax_examples.v | 72 +++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index 2da612f84..a20524348 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -362,10 +362,52 @@ Context {R : realType}. Inductive flag := D | P. +Section binop. + +Inductive binop := +| binop_and | binop_or +| binop_add | binop_minus | binop_mult. + +Definition type_of_binop (b : binop) : typ := +match b with +| binop_and => Bool +| binop_or => Bool +| binop_add => Real +| binop_minus => Real +| binop_mult => Real +end. + +(* Import Notations. *) + +Definition fun_of_binop g (b : binop) : (mctx g -> mtyp (type_of_binop b)) -> + (mctx g -> mtyp (type_of_binop b)) -> @mctx R g -> @mtyp R (type_of_binop b) := +match b with +| binop_and => (fun f1 f2 x => f1 x && f2 x : mtyp Bool) +| binop_or => (fun f1 f2 x => f1 x || f2 x : mtyp Bool) +| binop_add => (fun f1 f2 => (f1 \+ f2)%R) +| binop_minus => (fun f1 f2 => (f1 \- f2)%R) +| binop_mult => (fun f1 f2 => (f1 \* f2)%R) +end. + +Definition mfun_of_binop g b + (f1 : @mctx R g -> @mtyp R (type_of_binop b)) (mf1 : measurable_fun setT f1) + (f2 : @mctx R g -> @mtyp R (type_of_binop b)) (mf2 : measurable_fun setT f2) : + measurable_fun [set: @mctx R g] (fun_of_binop f1 f2). +destruct b. +exact: measurable_and mf1 mf2. +exact: measurable_or mf1 mf2. +exact: measurable_funD. +exact: measurable_funB. +exact: measurable_funM. +Defined. + +End binop. + Inductive exp : flag -> ctx -> typ -> Type := | exp_unit g : exp D g Unit | exp_bool g : bool -> exp D g Bool | exp_real g : R -> exp D g Real +| exp_bin g (b : binop) : exp D g (type_of_binop b) -> exp D g (type_of_binop b) -> exp D g (type_of_binop b) | exp_pair g t1 t2 : exp D g t1 -> exp D g t2 -> exp D g (Pair t1 t2) | exp_proj1 g t1 t2 : exp D g (Pair t1 t2) -> exp D g t1 | exp_proj2 g t1 t2 : exp D g (Pair t1 t2) -> exp D g t2 @@ -397,6 +439,7 @@ Arguments exp {R}. Arguments exp_unit {R g}. Arguments exp_bool {R g}. Arguments exp_real {R g}. +Arguments exp_bin {R g} &. Arguments exp_pair {R g} & {t1 t2}. Arguments exp_var {R g} _ {t} H. Arguments exp_bernoulli {R g}. @@ -417,6 +460,16 @@ Notation "b ':B'" := (@exp_bool _ _ b%bool) (in custom expr at level 1) : lang_scope. Notation "r ':R'" := (@exp_real _ _ r%R) (in custom expr at level 1, format "r :R") : lang_scope. +Notation "e1 && e2" := (exp_bin binop_and e1 e2) + (in custom expr at level 1) : lang_scope. +Notation "e1 || e2" := (exp_bin binop_or e1 e2) + (in custom expr at level 1) : lang_scope. +Notation "e1 + e2" := (exp_bin binop_add e1 e2) + (in custom expr at level 1) : lang_scope. +Notation "e1 - e2" := (exp_bin binop_minus e1 e2) + (in custom expr at level 1) : lang_scope. +Notation "e1 * e2" := (exp_bin binop_mult e1 e2) + (in custom expr at level 1) : lang_scope. Notation "'return' e" := (@exp_return _ _ _ e) (in custom expr at level 2) : lang_scope. (*Notation "% str" := (@exp_var _ _ str%string _ erefl) @@ -458,6 +511,7 @@ Fixpoint free_vars k g t (e : @exp R k g t) : seq string := | exp_unit _ => [::] | exp_bool _ _ => [::] | exp_real _ _ => [::] + | exp_bin _ _ e1 e2 => free_vars e1 ++ free_vars e2 | exp_pair _ _ _ e1 e2 => free_vars e1 ++ free_vars e2 | exp_proj1 _ _ _ e => free_vars e | exp_proj2 _ _ _ e => free_vars e @@ -575,6 +629,10 @@ Inductive evalD : forall g t, exp D g t -> | eval_real g r : ([r:R] : exp D g _) -D> cst r ; kr r +| eval_bin g bop (e1 : exp D g _) f1 mf1 e2 f2 mf2 : + e1 -D> f1 ; mf1 -> e2 -D> f2 ; mf2 -> + exp_bin bop e1 e2 -D> fun_of_binop f1 f2 ; mfun_of_binop mf1 mf2 + | eval_pair g t1 (e1 : exp D g t1) f1 mf1 t2 (e2 : exp D g t2) f2 mf2 : e1 -D> f1 ; mf1 -> e2 -D> f2 ; mf2 -> [(e1, e2)] -D> fun x => (f1 x, f2 x) ; measurable_fun_prod mf1 mf2 @@ -677,6 +735,12 @@ all: (rewrite {g t e u v mu mv hu}). - move=> g r {}v {}mv. inversion 1; subst g0 r0. by inj_ex H3. +- move=> g bop e1 f1 mf1 e2 f2 mf2 ev1 IH1 ev2 IH2 {}v {}mv. + inversion 1; subst g0 bop0. + inj_ex H10; subst v. + inj_ex H5; subst e1. + inj_ex H6; subst e5. + by move: H4 H11 => /IH1 <- /IH2 <-. - move=> g t1 e1 f1 mf1 t2 e2 f2 mf2 ev1 IH1 ev2 IH2 {}v {}mv. simple inversion 1 => //; subst g0. case: H3 => ? ?; subst t0 t3. @@ -799,6 +863,12 @@ all: rewrite {g t e u v eu}. - move=> g r {}v {}mv. inversion 1; subst g0 r0. by inj_ex H3. +- move=> g bop e1 f1 mf1 e2 f2 mf2 ev1 IH1 ev2 IH2 {}v {}mv. + inversion 1; subst g0 bop0. + inj_ex H10; subst v. + inj_ex H5; subst e1. + inj_ex H6; subst e5. + by move: H4 H11 => /IH1 <- /IH2 <-. - move=> g t1 e1 f1 mf1 t2 e2 f2 mf2 ev1 IH1 ev2 IH2 {}v {}mv. simple inversion 1 => //; subst g0. case: H3 => ? ?; subst t0 t3. @@ -915,6 +985,8 @@ all: rewrite {z g t}. - by do 2 eexists; exact: eval_unit. - by do 2 eexists; exact: eval_bool. - by do 2 eexists; exact: eval_real. +- move=> g b e1 [f1 [mf1 H1]] e2 [f2 [mf2 H2]]. + by exists (fun_of_binop f1 f2); eexists; exact: eval_bin. - move=> g t1 t2 e1 [f1 [mf1 H1]] e2 [f2 [mf2 H2]]. by exists (fun x => (f1 x, f2 x)); eexists; exact: eval_pair. - move=> g t1 t2 e [f [mf H]]. @@ -1023,6 +1095,15 @@ Proof. exact/execD_evalD/eval_bool. Qed. Lemma execD_real g r : @execD g _ [r:R] = existT _ (cst r) (kr r). Proof. exact/execD_evalD/eval_real. Qed. +Lemma execD_bin g bop (e1 : exp D g _) (e2 : exp D g _) : + let f1 := projT1 (execD e1) in let f2 := projT1 (execD e2) in + let mf1 := projT2 (execD e1) in let mf2 := projT2 (execD e2) in + execD (exp_bin bop e1 e2) = + @existT _ _ (fun_of_binop f1 f2) (mfun_of_binop mf1 mf2). +Proof. +by move=> f1 f2 mf1 mf2; apply/execD_evalD/eval_bin; exact: evalD_execD. +Qed. + Lemma execD_pair g t1 t2 (e1 : exp D g t1) (e2 : exp D g t2) : let f1 := projT1 (execD e1) in let f2 := projT1 (execD e2) in let mf1 := projT2 (execD e1) in let mf2 := projT2 (execD e2) in diff --git a/theories/lang_syntax_examples.v b/theories/lang_syntax_examples.v index c0881a54d..f2ef27315 100644 --- a/theories/lang_syntax_examples.v +++ b/theories/lang_syntax_examples.v @@ -257,6 +257,78 @@ rewrite exec_sample_pair0; do 3 rewrite mem_set//; rewrite memNset//=. by rewrite !mule1; congr (_%:E); field. Qed. +Definition sample_and_syntax0 : @exp R _ [::] _ := + [let "x" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in + let "y" := Sample {exp_bernoulli (1 / 3%:R)%:nng (p1S 2)} in + return #{"x"} && #{"y"}]. + +Lemma exec_sample_and0 (A : set bool) : + @execP R [::] _ sample_and_syntax0 tt A = + ((1 / 6)%:E * (true \in A)%:R%:E + + (1 - 1 / 6)%:E * (false \in A)%:R%:E)%E. +Proof. +rewrite !execP_letin !execP_sample !execD_bernoulli execP_return /=. +rewrite (@execD_bin _ _ binop_and) !exp_var'E (execD_var_erefl "x") (execD_var_erefl "y") /=. +rewrite letin'E integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !indicE !in_setT/= !mul1e. +rewrite !letin'E !integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !indicE !in_setT/= !mul1e !diracE. +rewrite muleDr// -addeA; congr (_ + _)%E. +by rewrite !muleA; congr (_%:E); congr (_ * _); field. +rewrite -muleDl// !muleA -muleDl//. +by congr (_%:E); congr (_ * _); field. +Qed. + +Definition sample_bernoulli_and3 : @exp R _ [::] _ := + [let "x" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in + let "y" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in + let "z" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in + return #{"x"} && #{"y"} && #{"z"}]. + +Lemma exec_sample_bernoulli_and3 t U : + execP sample_bernoulli_and3 t U = + ((1 / 8)%:E * (true \in U)%:R%:E + + (1 - 1 / 8)%:E * (false \in U)%:R%:E)%E. +Proof. +rewrite !execP_letin !execP_sample !execD_bernoulli execP_return /=. +rewrite !(@execD_bin _ _ binop_and) !exp_var'E. +rewrite (execD_var_erefl "x") (execD_var_erefl "y") (execD_var_erefl "z") /=. +rewrite letin'E integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !indicE !in_setT/= !mul1e. +rewrite !letin'E !integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !indicE !in_setT/= !mul1e. +rewrite !letin'E !integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !indicE !in_setT/= !mul1e !diracE. +rewrite !muleDr// -!addeA. +by congr (_ + _)%E; rewrite ?addeA !muleA -?muleDl//; +congr (_ * _)%E; congr (_%:E); field. +Qed. + +Definition sample_add_syntax0 : @exp R _ [::] _ := + [let "x" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in + let "y" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in + let "z" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in + return #{"x"} && #{"y"} && #{"z"}]. + +Lemma exec_sample_bernoulli_and3 t U : + execP sample_bernoulli_and3 t U = + ((1 / 8)%:E * (true \in U)%:R%:E + + (1 - 1 / 8)%:E * (false \in U)%:R%:E)%E. +Proof. +rewrite !execP_letin !execP_sample !execD_bernoulli execP_return /=. +rewrite !(@execD_bin _ _ binop_and) !exp_var'E. +rewrite (execD_var_erefl "x") (execD_var_erefl "y") (execD_var_erefl "z") /=. +rewrite letin'E integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !indicE !in_setT/= !mul1e. +rewrite !letin'E !integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !indicE !in_setT/= !mul1e. +rewrite !letin'E !integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !indicE !in_setT/= !mul1e !diracE. +rewrite !muleDr// -!addeA. +by congr (_ + _)%E; rewrite ?addeA !muleA -?muleDl//; +congr (_ * _)%E; congr (_%:E); field. +Qed. + End sample_pair. Section bernoulli_examples. From 59e14998dbf7115db3e6bef43cd3b1c0033cdc11 Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Tue, 10 Oct 2023 10:05:01 +0900 Subject: [PATCH 10/36] minor fixes, updates, rebases --- coq-mathcomp-analysis.opam | 3 +- theories/lang_syntax.v | 10 ++-- theories/lang_syntax_examples.v | 85 +++++++++++++-------------------- theories/lang_syntax_toy.v | 2 +- theories/prob_lang.v | 10 ++-- theories/prob_lang_wip.v | 4 +- 6 files changed, 44 insertions(+), 70 deletions(-) diff --git a/coq-mathcomp-analysis.opam b/coq-mathcomp-analysis.opam index b56eb46df..c7f50a6ee 100644 --- a/coq-mathcomp-analysis.opam +++ b/coq-mathcomp-analysis.opam @@ -19,7 +19,8 @@ depends: [ "coq-mathcomp-solvable" "coq-mathcomp-field" "coq-mathcomp-bigenough" { (>= "1.0.0") } - "coq-mathcomp-algebra-tactics" { (>= "1.1.1") } + "coq-mathcomp-algebra-tactics" { (>= "1.2.0") } + "coq-mathcomp-zify" { (>= "1.4.0") } ] tags: [ diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index a20524348..5b29062c3 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -1,8 +1,8 @@ Require Import String. From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. -From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. -From mathcomp.classical Require Import functions cardinality fsbigop. +From mathcomp Require Import mathcomp_extra boolp classical_sets. +From mathcomp Require Import functions cardinality fsbigop. From mathcomp Require Import signed reals ereal topology normedtype sequences. From mathcomp Require Import esum measure lebesgue_measure numfun. From mathcomp Require Import lebesgue_integral kernel prob_lang. @@ -190,8 +190,7 @@ HB.instance Definition _ z := @isMeasure.Build _ X R (T' z) (T0 z) (T_ge0 z) (@T_semi_sigma_additive z). Let sfinT z : sfinite_measure (T' z). Proof. exact: sfinite_kernel_measure. Qed. -HB.instance Definition _ z := @isSFinite.Build _ X R - (T' z) (sfinT z). +HB.instance Definition _ z := @isSFinite.Build _ X R (T' z) (sfinT z). Definition U' z : set Y -> \bar R := u z. Let U0 z : (U' z) set0 = 0. Proof. by []. Qed. @@ -202,8 +201,7 @@ HB.instance Definition _ z := @isMeasure.Build _ Y R (U' z) (U0 z) (U_ge0 z) (@U_semi_sigma_additive z). Let sfinU z : sfinite_measure (U' z). Proof. exact: sfinite_kernel_measure. Qed. -HB.instance Definition _ z := @isSFinite.Build _ Y R - (U' z) (sfinU z). +HB.instance Definition _ z := @isSFinite.Build _ Y R (U' z) (sfinU z). Lemma letin'C z A : measurable A -> letin' t diff --git a/theories/lang_syntax_examples.v b/theories/lang_syntax_examples.v index f2ef27315..922dd7be2 100644 --- a/theories/lang_syntax_examples.v +++ b/theories/lang_syntax_examples.v @@ -212,38 +212,38 @@ Definition sample_pair_syntax : exp _ [::] _ := Lemma exec_sample_pair0 (A : set (bool * bool)) : @execP R [::] _ sample_pair_syntax0 tt A = ((1 / 2)%:E * - ((1 / 3)%:E * ((true, true) \in A)%:R%:E + - (1 - 1 / 3)%:E * ((true, false) \in A)%:R%:E) + + ((1 / 3)%:E * \d_(true, true) A + + (1 - 1 / 3)%:E * \d_(true, false) A) + (1 - 1 / 2)%:E * - ((1 / 3)%:E * ((false, true) \in A)%:R%:E + - (1 - 1 / 3)%:E * ((false, false) \in A)%:R%:E))%E. + ((1 / 3)%:E * \d_(false, true) A + + (1 - 1 / 3)%:E * \d_(false, false) A))%E. Proof. rewrite !execP_letin !execP_sample !execD_bernoulli execP_return /=. rewrite execD_pair !exp_var'E (execD_var_erefl "x") (execD_var_erefl "y") /=. rewrite letin'E ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !diracE !in_setT/= !mul1e. +rewrite !integral_dirac//= !diracT !mul1e. rewrite !letin'E !ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. -by rewrite !integral_dirac//= !diracT !mul1e !diracE. +by rewrite !integral_dirac//= !diracT !mul1e. Qed. Lemma exec_sample_pair0_TandT : @execP R [::] _ sample_pair_syntax0 tt [set (true, true)] = (1 / 6)%:E. Proof. -rewrite exec_sample_pair0 mem_set//; do 3 rewrite memNset//=. +rewrite exec_sample_pair0 !diracE mem_set//; do 3 rewrite memNset//=. by rewrite /= !mule0 mule1 !add0e mule0 adde0; congr (_%:E); lra. Qed. Lemma exec_sample_pair0_TandF : @execP R [::] _ sample_pair_syntax0 tt [set (true, false)] = (1 / 3)%:E. Proof. -rewrite exec_sample_pair0 memNset// mem_set//; do 2 rewrite memNset//. +rewrite exec_sample_pair0 !diracE memNset// mem_set//; do 2 rewrite memNset//. by rewrite /= !mule0 mule1 !add0e mule0 adde0; congr (_%:E); lra. Qed. Lemma exec_sample_pair0_TandT' : @execP R [::] _ sample_pair_syntax0 tt [set p | p.1 && p.2] = (1 / 6)%:E. Proof. -rewrite exec_sample_pair0 mem_set//; do 3 rewrite memNset//=. +rewrite exec_sample_pair0 !diracE mem_set//; do 3 rewrite memNset//=. by rewrite /= !mule0 mule1 !add0e mule0 adde0; congr (_%:E); lra. Qed. @@ -251,9 +251,9 @@ Lemma exec_sample_pair_TorT : (projT1 (execD sample_pair_syntax)) tt [set p | p.1 || p.2] = (2 / 3)%:E. Proof. rewrite execD_normalize_pt normalizeE/= exec_sample_pair0. -do 4 rewrite mem_set//=. +rewrite !diracE; do 4 rewrite mem_set//=. rewrite eqe ifF; last by apply/negbTE/negP => /orP[/eqP|//]; lra. -rewrite exec_sample_pair0; do 3 rewrite mem_set//; rewrite memNset//=. +rewrite exec_sample_pair0 !diracE; do 3 rewrite mem_set//; rewrite memNset//=. by rewrite !mule1; congr (_%:E); field. Qed. @@ -263,18 +263,17 @@ Definition sample_and_syntax0 : @exp R _ [::] _ := return #{"x"} && #{"y"}]. Lemma exec_sample_and0 (A : set bool) : - @execP R [::] _ sample_and_syntax0 tt A = - ((1 / 6)%:E * (true \in A)%:R%:E + - (1 - 1 / 6)%:E * (false \in A)%:R%:E)%E. + @execP R [::] _ sample_and_syntax0 tt A = ((1 / 6)%:E * \d_true A + + (1 - 1 / 6)%:E * \d_false A)%E. Proof. rewrite !execP_letin !execP_sample !execD_bernoulli execP_return /=. rewrite (@execD_bin _ _ binop_and) !exp_var'E (execD_var_erefl "x") (execD_var_erefl "y") /=. -rewrite letin'E integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !indicE !in_setT/= !mul1e. -rewrite !letin'E !integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !indicE !in_setT/= !mul1e !diracE. +rewrite letin'E ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !diracT !mul1e. +rewrite !letin'E !ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !diracT !mul1e. rewrite muleDr// -addeA; congr (_ + _)%E. -by rewrite !muleA; congr (_%:E); congr (_ * _); field. + by rewrite !muleA; congr (_%:E); congr (_ * _); field. rewrite -muleDl// !muleA -muleDl//. by congr (_%:E); congr (_ * _); field. Qed. @@ -286,19 +285,18 @@ Definition sample_bernoulli_and3 : @exp R _ [::] _ := return #{"x"} && #{"y"} && #{"z"}]. Lemma exec_sample_bernoulli_and3 t U : - execP sample_bernoulli_and3 t U = - ((1 / 8)%:E * (true \in U)%:R%:E + - (1 - 1 / 8)%:E * (false \in U)%:R%:E)%E. + execP sample_bernoulli_and3 t U = ((1 / 8)%:E * \d_true U + + (1 - 1 / 8)%:E * \d_false U)%E. Proof. rewrite !execP_letin !execP_sample !execD_bernoulli execP_return /=. rewrite !(@execD_bin _ _ binop_and) !exp_var'E. rewrite (execD_var_erefl "x") (execD_var_erefl "y") (execD_var_erefl "z") /=. -rewrite letin'E integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !indicE !in_setT/= !mul1e. -rewrite !letin'E !integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !indicE !in_setT/= !mul1e. -rewrite !letin'E !integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !indicE !in_setT/= !mul1e !diracE. +rewrite letin'E ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !diracT !mul1e. +rewrite !letin'E !ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !diracT !mul1e. +rewrite !letin'E !ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. +rewrite !integral_dirac//= !diracT !mul1e. rewrite !muleDr// -!addeA. by congr (_ + _)%E; rewrite ?addeA !muleA -?muleDl//; congr (_ * _)%E; congr (_%:E); field. @@ -310,25 +308,6 @@ Definition sample_add_syntax0 : @exp R _ [::] _ := let "z" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in return #{"x"} && #{"y"} && #{"z"}]. -Lemma exec_sample_bernoulli_and3 t U : - execP sample_bernoulli_and3 t U = - ((1 / 8)%:E * (true \in U)%:R%:E + - (1 - 1 / 8)%:E * (false \in U)%:R%:E)%E. -Proof. -rewrite !execP_letin !execP_sample !execD_bernoulli execP_return /=. -rewrite !(@execD_bin _ _ binop_and) !exp_var'E. -rewrite (execD_var_erefl "x") (execD_var_erefl "y") (execD_var_erefl "z") /=. -rewrite letin'E integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !indicE !in_setT/= !mul1e. -rewrite !letin'E !integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !indicE !in_setT/= !mul1e. -rewrite !letin'E !integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !indicE !in_setT/= !mul1e !diracE. -rewrite !muleDr// -!addeA. -by congr (_ + _)%E; rewrite ?addeA !muleA -?muleDl//; -congr (_ * _)%E; congr (_%:E); field. -Qed. - End sample_pair. Section bernoulli_examples. @@ -371,12 +350,12 @@ rewrite -EFinD/= eqe ifF; last first. rewrite !letin'E/= !iteE/=. rewrite !ge0_integral_mscale//=. rewrite ger0_norm//. -rewrite !integral_dirac//= !diracT /= !mul1e ger0_norm// !indicT. +rewrite !integral_dirac//= !diracT !mul1e ger0_norm//. rewrite exp_var'E (execD_var_erefl "x")/=. rewrite /bernoulli/= measure_addE/= /mscale/= !mul1r. by rewrite muleDl//; congr (_ + _)%E; rewrite -!EFinM; congr (_%:E); - rewrite !indicE /onem /=; case: (_ \in _); field. + rewrite !indicT !indicE /onem /=; case: (_ \in _); field. Qed. Definition bernoulli12_score := [Normalize @@ -414,13 +393,13 @@ rewrite -EFinD/= eqe ifF; last first. rewrite !letin'E/= !iteE/=. rewrite !ge0_integral_mscale//=. rewrite ger0_norm//. -rewrite !integral_dirac//= !diracT /= !mul1e ger0_norm// !indicT. +rewrite !integral_dirac//= !diracT !mul1e ger0_norm//. rewrite exp_var'E (execD_var_erefl "x")/=. rewrite /bernoulli/= measure_addE/= /mscale/= !mul1r. rewrite muleDl//; congr (_ + _)%E; rewrite -!EFinM; congr (_%:E); - by rewrite indicE /onem; case: (_ \in _); field. + by rewrite !indicT !indicE /onem /=; case: (_ \in _); field. Qed. (* https://dl.acm.org/doi/pdf/10.1145/2933575.2935313 (Sect. 4) *) @@ -464,11 +443,11 @@ rewrite !letin'E/= !iteE/=. rewrite !ge0_integral_mscale//=. rewrite ger0_norm//. rewrite !integral_dirac//= !diracT !mul1e ger0_norm//. -rewrite /bernoulli/= measure_addE/= /mscale/=. +rewrite /bernoulli/= measure_addE/= /mscale/= !mul1r. rewrite muleDl//; congr (_ + _)%E; rewrite -!EFinM; congr (_%:E); - by rewrite indicE /onem; case: (_ \in _); field. + by rewrite !indicE /onem /=; case: (_ \in _); field. Qed. End bernoulli_examples. diff --git a/theories/lang_syntax_toy.v b/theories/lang_syntax_toy.v index 350a3d271..149b7640c 100644 --- a/theories/lang_syntax_toy.v +++ b/theories/lang_syntax_toy.v @@ -1,7 +1,7 @@ Require Import String Classical. From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg. -From mathcomp.classical Require Import mathcomp_extra boolp. +From mathcomp Require Import mathcomp_extra boolp. From mathcomp Require Import signed reals topology normedtype. From mathcomp Require Import lang_syntax_util. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 145994e05..9499d9317 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -2,7 +2,7 @@ From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval finmap. From mathcomp Require Import rat archimedean. -From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. +From mathcomp Require Import mathcomp_extra boolp classical_sets. From mathcomp.classical Require Import functions cardinality fsbigop. From mathcomp Require Import reals ereal signed topology normedtype sequences. From mathcomp Require Import esum measure lebesgue_measure numfun. @@ -88,7 +88,6 @@ Qed. Section bernoulli. Variables (R : realType) (p : {nonneg R}) (p1 : (p%:num <= 1)%R). -Local Open Scope ring_scope. Definition bernoulli : set _ -> \bar R := measure_add @@ -97,8 +96,6 @@ Definition bernoulli : set _ -> \bar R := HB.instance Definition _ := Measure.on bernoulli. -Local Close Scope ring_scope. - Let bernoulli_setT : bernoulli [set: _] = 1. Proof. rewrite /bernoulli/= /measure_add/= /msum 2!big_ord_recr/= big_ord0 add0e/=. @@ -1020,8 +1017,7 @@ HB.instance Definition _ z := @isMeasure.Build _ Y R (U z) (U0 z) (U_ge0 z) (@U_semi_sigma_additive z). Let sfinU z : sfinite_measure (U z). Proof. exact: sfinite_kernel_measure. Qed. -HB.instance Definition _ z := @isSFinite.Build _ Y R - (U z) (sfinU z). +HB.instance Definition _ z := @isSFinite.Build _ Y R (U z) (sfinU z). Lemma letinC z A : measurable A -> letin t @@ -1055,7 +1051,7 @@ End letinC. Section constants. Variable R : realType. -Local Open Scope ring_scope. +Local Close Scope ereal_scope. Lemma onem1S n : `1- (1 / n.+1%:R) = (n%:R / n.+1%:R)%:nng%:num :> R. Proof. diff --git a/theories/prob_lang_wip.v b/theories/prob_lang_wip.v index 8c6c5a33e..f07c4abf4 100644 --- a/theories/prob_lang_wip.v +++ b/theories/prob_lang_wip.v @@ -1,8 +1,8 @@ From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval finmap. From mathcomp Require Import rat. -From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. -From mathcomp.classical Require Import functions cardinality fsbigop. +From mathcomp Require Import mathcomp_extra boolp classical_sets. +From mathcomp Require Import functions cardinality fsbigop. From mathcomp Require Import signed reals ereal topology normedtype sequences. From mathcomp Require Import esum measure lebesgue_measure numfun. From mathcomp Require Import lebesgue_integral exp kernel trigo prob_lang. From 244242169faa1cdd232571821b4023900ed614aa Mon Sep 17 00:00:00 2001 From: AyumuSaito Date: Tue, 14 Feb 2023 22:02:38 +0900 Subject: [PATCH 11/36] casino example and extensions - binomial - uniform - generalize measure_and - change the order of binop & add pow - fix for mR - rewrite casino0 to casino1 Co-authored-by: Reynald Affeldt --- theories/lang_syntax.v | 286 ++++++++++++++++++---- theories/lang_syntax_examples.v | 118 ++++++---- theories/lang_syntax_examples_wip.v | 329 ++++++++++++++++++++++++++ theories/prob_lang.v | 354 +++++++++++++++++++++++----- theories/prob_lang_wip.v | 2 +- 5 files changed, 924 insertions(+), 165 deletions(-) create mode 100644 theories/lang_syntax_examples_wip.v diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index 5b29062c3..4a87005e6 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -276,7 +276,9 @@ Fixpoint measurable_of_typ (t : typ) : {d & measurableType d} := match t with | Unit => existT _ _ munit | Bool => existT _ _ mbool - | Real => existT _ _ (mR R) + | Real => existT _ _ + [the measurableType _ of (@measurableTypeR R)] + (* (Real_sort__canonical__measure_Measurable R) *) | Pair A B => existT _ _ [the measurableType (projT1 (measurable_of_typ A), projT1 (measurable_of_typ B)).-prod%mdisp of @@ -378,7 +380,7 @@ end. (* Import Notations. *) Definition fun_of_binop g (b : binop) : (mctx g -> mtyp (type_of_binop b)) -> - (mctx g -> mtyp (type_of_binop b)) -> @mctx R g -> @mtyp R (type_of_binop b) := + (mctx g -> mtyp (type_of_binop b)) -> @mctx R g -> @mtyp R (type_of_binop b) := match b with | binop_and => (fun f1 f2 x => f1 x && f2 x : mtyp Bool) | binop_or => (fun f1 f2 x => f1 x || f2 x : mtyp Bool) @@ -387,8 +389,8 @@ match b with | binop_mult => (fun f1 f2 => (f1 \* f2)%R) end. -Definition mfun_of_binop g b - (f1 : @mctx R g -> @mtyp R (type_of_binop b)) (mf1 : measurable_fun setT f1) +Definition mfun_of_binop g b + (f1 : @mctx R g -> @mtyp R (type_of_binop b)) (mf1 : measurable_fun setT f1) (f2 : @mctx R g -> @mtyp R (type_of_binop b)) (mf2 : measurable_fun setT f2) : measurable_fun [set: @mctx R g] (fun_of_binop f1 f2). destruct b. @@ -401,17 +403,52 @@ Defined. End binop. +Section relop. +Inductive relop := +| relop_le | relop_lt | relop_eq . + +Definition fun_of_relop g (r : relop) : (@mctx R g -> @mtyp R Real) -> + (mctx g -> mtyp Real) -> @mctx R g -> @mtyp R Bool := +match r with +| relop_le => (fun f1 f2 x => (f1 x <= f2 x)%R) +| relop_lt => (fun f1 f2 x => (f1 x < f2 x)%R) +| relop_eq => (fun f1 f2 x => (f1 x == f2 x)%R) +end. + +Definition mfun_of_relop g r + (f1 : @mctx R g -> @mtyp R Real) (mf1 : measurable_fun setT f1) + (f2 : @mctx R g -> @mtyp R Real) (mf2 : measurable_fun setT f2) : + measurable_fun [set: @mctx R g] (fun_of_relop r f1 f2). +destruct r. +exact: measurable_fun_ler. +exact: measurable_fun_ltr. +exact: measurable_fun_eqr. +Defined. + +End relop. + Inductive exp : flag -> ctx -> typ -> Type := | exp_unit g : exp D g Unit | exp_bool g : bool -> exp D g Bool | exp_real g : R -> exp D g Real -| exp_bin g (b : binop) : exp D g (type_of_binop b) -> exp D g (type_of_binop b) -> exp D g (type_of_binop b) +| exp_pow g : nat -> exp D g Real -> exp D g Real +| exp_bin (b : binop) g : exp D g (type_of_binop b) -> + exp D g (type_of_binop b) -> exp D g (type_of_binop b) +| exp_rel (r : relop) g : exp D g Real -> + exp D g Real -> exp D g Bool | exp_pair g t1 t2 : exp D g t1 -> exp D g t2 -> exp D g (Pair t1 t2) | exp_proj1 g t1 t2 : exp D g (Pair t1 t2) -> exp D g t1 | exp_proj2 g t1 t2 : exp D g (Pair t1 t2) -> exp D g t2 | exp_var g str t : t = lookup Unit g str -> exp D g t | exp_bernoulli g (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : exp D g (Prob Bool) +| exp_bernoulli_trunc g : + exp D g Real -> exp D g (Prob Bool) +| exp_binomial g (n : nat) (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : + exp D g (Prob Real) +| exp_binomial_trunc g (n : nat) : + exp D g Real -> exp D g (Prob Real) +| exp_uniform g (a b : R) (ab0 : (0 < b - a)%R) : exp D g (Prob Real) | exp_poisson g : nat -> exp D g Real -> exp D g Real | exp_normalize g t : exp P g t -> exp D g (Prob t) | exp_letin g t1 t2 str : exp P g t1 -> exp P ((str, t1) :: g) t2 -> @@ -437,19 +474,25 @@ Arguments exp {R}. Arguments exp_unit {R g}. Arguments exp_bool {R g}. Arguments exp_real {R g}. -Arguments exp_bin {R g} &. +Arguments exp_pow {R g}. +Arguments exp_bin {R} b {g} &. +Arguments exp_rel {R} r {g} &. Arguments exp_pair {R g} & {t1 t2}. -Arguments exp_var {R g} _ {t} H. +Arguments exp_var {R g} _ {t} & H. Arguments exp_bernoulli {R g}. +Arguments exp_bernoulli_trunc {R g} &. +Arguments exp_binomial {R g}. +Arguments exp_uniform {R g} &. +Arguments exp_binomial_trunc {R g} &. Arguments exp_poisson {R g}. Arguments exp_normalize {R g _}. Arguments exp_letin {R g} & {_ _}. -Arguments exp_sample {R g t}. +Arguments exp_sample {R g} & {t}. Arguments exp_score {R g}. Arguments exp_return {R g} & {_}. -Arguments exp_if {R z g t}. +Arguments exp_if {R z g t} &. Arguments exp_weak {R} z g h {t} x. -Arguments exp_var' {R} str {t} g. +Arguments exp_var' {R} str {t} g &. Declare Custom Entry expr. Notation "[ e ]" := e (e custom expr at level 5) : lang_scope. @@ -458,18 +501,24 @@ Notation "b ':B'" := (@exp_bool _ _ b%bool) (in custom expr at level 1) : lang_scope. Notation "r ':R'" := (@exp_real _ _ r%R) (in custom expr at level 1, format "r :R") : lang_scope. -Notation "e1 && e2" := (exp_bin binop_and e1 e2) +Notation "e ^+ n" := (exp_pow n e) (in custom expr at level 1) : lang_scope. +Notation "e1 && e2" := (exp_bin binop_and e1 e2) + (in custom expr at level 2) : lang_scope. Notation "e1 || e2" := (exp_bin binop_or e1 e2) - (in custom expr at level 1) : lang_scope. + (in custom expr at level 2) : lang_scope. Notation "e1 + e2" := (exp_bin binop_add e1 e2) - (in custom expr at level 1) : lang_scope. + (in custom expr at level 3) : lang_scope. Notation "e1 - e2" := (exp_bin binop_minus e1 e2) - (in custom expr at level 1) : lang_scope. + (in custom expr at level 3) : lang_scope. Notation "e1 * e2" := (exp_bin binop_mult e1 e2) - (in custom expr at level 1) : lang_scope. -Notation "'return' e" := (@exp_return _ _ _ e) (in custom expr at level 2) : lang_scope. +Notation "e1 <= e2" := (exp_rel relop_le e1 e2) + (in custom expr at level 2) : lang_scope. +Notation "e1 == e2" := (exp_rel relop_eq e1 e2) + (in custom expr at level 4) : lang_scope. +Notation "'return' e" := (@exp_return _ _ _ e) + (in custom expr at level 6) : lang_scope. (*Notation "% str" := (@exp_var _ _ str%string _ erefl) (in custom expr at level 1, format "% str") : lang_scope.*) (* Notation "% str H" := (@exp_var _ _ str%string _ H) @@ -485,21 +534,21 @@ Notation "\pi_1 e" := (exp_proj1 e) Notation "\pi_2 e" := (exp_proj2 e) (in custom expr at level 1) : lang_scope. Notation "'let' x ':=' e 'in' f" := (exp_letin x e f) - (in custom expr at level 3, + (in custom expr at level 5, x constr, - f custom expr at level 3, + f custom expr at level 5, left associativity) : lang_scope. Notation "{ c }" := c (in custom expr, c constr) : lang_scope. Notation "x" := x (in custom expr at level 0, x ident) : lang_scope. Notation "'Sample' e" := (exp_sample e) - (in custom expr at level 2) : lang_scope. + (in custom expr at level 5) : lang_scope. Notation "'Score' e" := (exp_score e) - (in custom expr at level 2) : lang_scope. + (in custom expr at level 5) : lang_scope. Notation "'Normalize' e" := (exp_normalize e) (in custom expr at level 0) : lang_scope. Notation "'if' e1 'then' e2 'else' e3" := (exp_if e1 e2 e3) - (in custom expr at level 1) : lang_scope. + (in custom expr at level 6) : lang_scope. Section free_vars. Context {R : realType}. @@ -509,12 +558,18 @@ Fixpoint free_vars k g t (e : @exp R k g t) : seq string := | exp_unit _ => [::] | exp_bool _ _ => [::] | exp_real _ _ => [::] + | exp_pow _ _ e => free_vars e | exp_bin _ _ e1 e2 => free_vars e1 ++ free_vars e2 + | exp_rel _ _ e1 e2 => free_vars e1 ++ free_vars e2 | exp_pair _ _ _ e1 e2 => free_vars e1 ++ free_vars e2 | exp_proj1 _ _ _ e => free_vars e | exp_proj2 _ _ _ e => free_vars e | exp_var _ x _ _ => [:: x] | exp_bernoulli _ _ _ => [::] + | exp_bernoulli_trunc _ e => free_vars e + | exp_binomial _ _ _ _ => [::] + | exp_uniform _ _ _ _ => [::] + | exp_binomial_trunc _ _ e => free_vars e | exp_poisson _ _ e => free_vars e | exp_normalize _ _ e => free_vars e | exp_letin _ _ _ x e1 e2 => free_vars e1 ++ rem x (free_vars e2) @@ -619,6 +674,20 @@ Context {R : realType}. Implicit Type (g : ctx) (str : string). Local Open Scope lang_scope. +Local Open Scope ring_scope. +Definition bernoulli0 := @bernoulli R 0%R%:nng ler01. + +HB.instance Definition _ := Probability.on bernoulli0. + +Lemma __ : Measurable.sort + (pprobability + [the measurableType (R.-ocitv.-measurable).-sigma of + g_sigma_algebraType (R.-ocitv.-measurable)] R) = +Measurable.sort (@mtyp R (Prob Real)). +rewrite /=. +(* done. *) +Abort. + Inductive evalD : forall g t, exp D g t -> forall f : dval R g t, measurable_fun setT f -> Prop := | eval_unit g : ([TT] : exp D g _) -D> cst tt ; ktt @@ -627,10 +696,17 @@ Inductive evalD : forall g t, exp D g t -> | eval_real g r : ([r:R] : exp D g _) -D> cst r ; kr r +| eval_pow g n (e : exp D g _) f mf : e -D> f ; mf -> + [e ^+ {n}] -D> (fun x => f x ^+ n) ; (measurable_funX n mf) + | eval_bin g bop (e1 : exp D g _) f1 mf1 e2 f2 mf2 : e1 -D> f1 ; mf1 -> e2 -D> f2 ; mf2 -> exp_bin bop e1 e2 -D> fun_of_binop f1 f2 ; mfun_of_binop mf1 mf2 +| eval_rel g rop (e1 : exp D g _) f1 mf1 e2 f2 mf2 : + e1 -D> f1 ; mf1 -> e2 -D> f2 ; mf2 -> + exp_rel rop e1 e2 -D> fun_of_relop rop f1 f2 ; mfun_of_relop rop mf1 mf2 + | eval_pair g t1 (e1 : exp D g t1) f1 mf1 t2 (e2 : exp D g t2) f2 mf2 : e1 -D> f1 ; mf1 -> e2 -D> f2 ; mf2 -> [(e1, e2)] -D> fun x => (f1 x, f2 x) ; measurable_fun_prod mf1 mf2 @@ -649,9 +725,27 @@ Inductive evalD : forall g t, exp D g t -> | eval_var g x H : let i := index x (dom g) in exp_var x H -D> acc_typ (map snd g) i ; measurable_acc_typ (map snd g) i -| eval_bernoulli g (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : +| eval_bernoulli g r r1 : (exp_bernoulli r r1 : exp D g _) -D> cst (bernoulli r1) ; - measurable_cst _ + measurable_cst _ + +| eval_bernoulli_trunc g e r mr : + e -D> r ; mr -> + (exp_bernoulli_trunc e : exp D g _) -D> bernoulli_trunc \o r ; + measurableT_comp measurable_bernoulli_trunc mr + +| eval_binomial g n (p : {nonneg R}) (p1 : (p%:num <= 1)%R) : + (exp_binomial n p p1 : exp D g _) -D> cst (binomial_probability n p1) ; + measurable_cst _ + +| eval_binomial_trunc g n e r mr : + e -D> r ; mr -> + (exp_binomial_trunc n e : exp D g _) -D> binomial_probability_trunc n \o r ; + measurableT_comp (measurable_binomial_probability_trunc n) mr + +| eval_uniform g (a b : R) (ab0 : (0 < b - a)%R) : + (exp_uniform a b ab0 : exp D g _) -D> cst (uniform_probability ab0) ; + measurable_cst _ | eval_poisson g n (e : exp D g _) f mf : e -D> f ; mf -> @@ -681,8 +775,8 @@ with evalP : forall g t, exp P g t -> pval R g t -> Prop := [let str := e1 in e2] -P> letin' k1 k2 | eval_sample g t (e : exp _ _ (Prob t)) - (f : mctx g -> probability (mtyp t) R) mf : - e -D> f ; mf -> [Sample e] -P> sample f mf + (p : mctx g -> pprobability (mtyp t) R) mp : + e -D> p ; mp -> [Sample e] -P> sample p mp | eval_score g (e : exp _ g _) f mf : e -D> f ; mf -> [Score e] -P> kscore mf @@ -733,12 +827,23 @@ all: (rewrite {g t e u v mu mv hu}). - move=> g r {}v {}mv. inversion 1; subst g0 r0. by inj_ex H3. +- move=> g n e f mf ev IH {}v {}mv. + inversion 1; subst g0 n0. + inj_ex H4; subst v. + inj_ex H2; subst e0. + by move: H3 => /IH <-. - move=> g bop e1 f1 mf1 e2 f2 mf2 ev1 IH1 ev2 IH2 {}v {}mv. inversion 1; subst g0 bop0. inj_ex H10; subst v. inj_ex H5; subst e1. inj_ex H6; subst e5. by move: H4 H11 => /IH1 <- /IH2 <-. +- move=> g rop e1 f1 mf1 e2 f2 mf2 ev1 IH1 ev2 IH2 {}v {}mv. + inversion 1; subst g0 rop0. + inj_ex H5; subst v. + inj_ex H1; subst e1. + inj_ex H3; subst e3. + by move: H6 H7 => /IH1 <- /IH2 <-. - move=> g t1 e1 f1 mf1 t2 e2 f2 mf2 ev1 IH1 ev2 IH2 {}v {}mv. simple inversion 1 => //; subst g0. case: H3 => ? ?; subst t0 t3. @@ -759,22 +864,33 @@ all: (rewrite {g t e u v mu mv hu}). clear H9. inj_ex H7; subst e1. by rewrite (ih _ _ H4). -(* - move=> g str n {}v {}mv. - inversion 1; subst g0. - inj_ex H6; rewrite -H6. - by inj_ex H7. - inj_ex H8; rewrite -H8. - by inj_ex H9. *) - move=> g str H n {}v {}mv. inversion 1; subst g0. - (* inj_ex H7; rewrite -H7. - by inj_ex H8. *) inj_ex H9; rewrite -H9. by inj_ex H10. - move=> g r r1 {}v {}mv. inversion 1; subst g0 r0. inj_ex H3; subst v. by have -> : r1 = r3 by []. +- move=> g e r mr ev IH {}v {}mv. + inversion 1; subst g0. + inj_ex H0; subst e0. + inj_ex H4; subst v. + by rewrite (IH _ _ H2). +- move=> g n p p1 {}v {}mv. + inversion 1; subst g0 n0 p0. + inj_ex H4; subst v. + by have -> : p1 = p3 by []. +- move=> g n e f mf ev IH {}v {}mv. + inversion 1; subst g0 n0. + inj_ex H2; subst e0. + inj_ex H4; subst v. + inj_ex H5; subst mv. + by rewrite (IH _ _ H3). +- move=> g a b ab0 {}v {}mv. + inversion 1; subst g0 a0 b0. + inj_ex H4; subst v. + by have -> : ab0 = ab2. - move=> g n e0 f mf ev IH {}v {}mv. inversion 1; subst g0 n0. inj_ex H2; subst e0. @@ -807,13 +923,13 @@ all: (rewrite {g t e u v mu mv hu}). inj_ex H6; subst e5. inj_ex H5; subst e4. by rewrite (IH1 _ H4) (IH2 _ H8). -- move=> g t e f mf ev IH k. +- move=> g t e p mp ev IH k. inversion 1; subst g0. inj_ex H5; subst t0. inj_ex H5; subst e1. inj_ex H7; subst k. - have ? := IH _ _ H3; subst f1. - by have -> : mf = mf1 by []. + have ? := IH _ _ H3; subst p1. + by have -> : mp = mp1 by []. - move=> g e f mf ev IH k. inversion 1; subst g0. inj_ex H0; subst e0. @@ -861,12 +977,23 @@ all: rewrite {g t e u v eu}. - move=> g r {}v {}mv. inversion 1; subst g0 r0. by inj_ex H3. +- move=> g n e f mf ev IH {}v {}mv. + inversion 1; subst g0 n0. + inj_ex H4; subst v. + inj_ex H2; subst e0. + by move: H3 => /IH <-. - move=> g bop e1 f1 mf1 e2 f2 mf2 ev1 IH1 ev2 IH2 {}v {}mv. inversion 1; subst g0 bop0. inj_ex H10; subst v. inj_ex H5; subst e1. inj_ex H6; subst e5. by move: H4 H11 => /IH1 <- /IH2 <-. +- move=> g rop e1 f1 mf1 e2 f2 mf2 ev1 IH1 ev2 IH2 {}v {}mv. + inversion 1; subst g0 rop0. + inj_ex H5; subst v. + inj_ex H1; subst e1. + inj_ex H3; subst e3. + by move: H6 H7 => /IH1 <- /IH2 <-. - move=> g t1 e1 f1 mf1 t2 e2 f2 mf2 ev1 IH1 ev2 IH2 {}v {}mv. simple inversion 1 => //; subst g0. case: H3 => ? ?; subst t0 t3. @@ -888,22 +1015,33 @@ all: rewrite {g t e u v eu}. clear H9. inj_ex H7; subst e1. by rewrite (ih _ _ H4). -(* - move=> g str n {}v {}mv. - inversion 1; subst g0. - inj_ex H6; rewrite -H6. - by inj_ex H7. - inj_ex H8; rewrite -H8. - by inj_ex H9. *) - move=> g str H n {}v {}mv. inversion 1; subst g0. - (* inj_ex H7; rewrite -H7. - by inj_ex H8. *) inj_ex H9; rewrite -H9. by inj_ex H10. - move=> g r r1 {}v {}mv. inversion 1; subst g0 r0. inj_ex H3; subst v. by have -> : r1 = r3 by []. +- move=> g e r mr ev IH {}v {}mv. + inversion 1; subst g0. + inj_ex H0; subst e0. + inj_ex H4; subst v. + by rewrite (IH _ _ H2). +- move=> g n p p1 {}v {}mv. + inversion 1; subst g0 n0 p0. + inj_ex H4; subst v. + by have -> : p1 = p3 by []. +- move=> g n e f mf ev IH {}v {}mv. + inversion 1; subst g0 n0. + inj_ex H2; subst e0. + inj_ex H4; subst v. + inj_ex H5; subst mv. + by rewrite (IH _ _ H3). +- move=> g a b ab0 {}v {}mv. + inversion 1; subst g0 a0 b0. + inj_ex H4; subst v. + by have -> : ab0 = ab2. - move=> g n e f mf ev IH {}v {}mv. inversion 1; subst g0 n0. inj_ex H2; subst e0. @@ -938,12 +1076,12 @@ all: rewrite {g t e u v eu}. inj_ex H5; subst e4. inj_ex H6; subst e5. by rewrite (IH1 _ H4) (IH2 _ H8). -- move=> g t e f mf ep IH v. +- move=> g t e p mp ep IH v. inversion 1; subst g0 t0. inj_ex H7; subst v. inj_ex H5; subst e1. - have ? := IH _ _ H3; subst f1. - by have -> : mf = mf1 by []. + have ? := IH _ _ H3; subst p1. + by have -> : mp = mp1 by []. - move=> g e f mf ev IH k. inversion 1; subst g0. inj_ex H0; subst e0. @@ -983,8 +1121,12 @@ all: rewrite {z g t}. - by do 2 eexists; exact: eval_unit. - by do 2 eexists; exact: eval_bool. - by do 2 eexists; exact: eval_real. -- move=> g b e1 [f1 [mf1 H1]] e2 [f2 [mf2 H2]]. +- move=> g n e [f [mf H]]. + by exists (fun x => (f x ^+ n)%R); eexists; exact: eval_pow. +- move=> b g e1 [f1 [mf1 H1]] e2 [f2 [mf2 H2]]. by exists (fun_of_binop f1 f2); eexists; exact: eval_bin. +- move=> r g e1 [f1 [mf1 H1]] e2 [f2 [mf2 H2]]. + by exists (fun_of_relop r f1 f2); eexists; exact: eval_rel. - move=> g t1 t2 e1 [f1 [mf1 H1]] e2 [f2 [mf2 H2]]. by exists (fun x => (f1 x, f2 x)); eexists; exact: eval_pair. - move=> g t1 t2 e [f [mf H]]. @@ -993,6 +1135,13 @@ all: rewrite {z g t}. by exists (snd \o f); eexists; exact: eval_proj2. - by move=> g x t tE; subst t; eexists; eexists; exact: eval_var. - by move=> r r1; eexists; eexists; exact: eval_bernoulli. +- move=> g e [p [mp H]]. + by exists (bernoulli_trunc \o p); eexists; exact: eval_bernoulli_trunc. +- by move=> p p1; eexists; eexists; exact: eval_binomial. +- move=> g n e [p [mp H]]. + exists (binomial_probability_trunc n \o p). + eexists; exact: (eval_binomial_trunc n). +- by eexists; eexists; exact: eval_uniform. - move=> g h e [f [mf H]]. by exists (poisson h \o f); eexists; exact: eval_poisson. - move=> g t e [k ek]. @@ -1093,13 +1242,31 @@ Proof. exact/execD_evalD/eval_bool. Qed. Lemma execD_real g r : @execD g _ [r:R] = existT _ (cst r) (kr r). Proof. exact/execD_evalD/eval_real. Qed. +Local Open Scope ring_scope. +Lemma execD_pow g (e : exp D g _) n : + let f := projT1 (execD e) in let mf := projT2 (execD e) in + execD (exp_pow n e) = + @existT _ _ (fun x => f x ^+ n) (measurable_funX n mf). +Proof. +by move=> f mf; apply/execD_evalD/eval_pow/evalD_execD. +Qed. + Lemma execD_bin g bop (e1 : exp D g _) (e2 : exp D g _) : let f1 := projT1 (execD e1) in let f2 := projT1 (execD e2) in let mf1 := projT2 (execD e1) in let mf2 := projT2 (execD e2) in execD (exp_bin bop e1 e2) = @existT _ _ (fun_of_binop f1 f2) (mfun_of_binop mf1 mf2). Proof. -by move=> f1 f2 mf1 mf2; apply/execD_evalD/eval_bin; exact: evalD_execD. +by move=> f1 f2 mf1 mf2; apply/execD_evalD/eval_bin; exact/evalD_execD. +Qed. + +Lemma execD_rel g rop (e1 : exp D g _) (e2 : exp D g _) : + let f1 := projT1 (execD e1) in let f2 := projT1 (execD e2) in + let mf1 := projT2 (execD e1) in let mf2 := projT2 (execD e2) in + execD (exp_rel rop e1 e2) = + @existT _ _ (fun_of_relop rop f1 f2) (mfun_of_relop rop mf1 mf2). +Proof. +by move=> f1 f2 mf1 mf2; apply/execD_evalD/eval_rel; exact: evalD_execD. Qed. Lemma execD_pair g t1 t2 (e1 : exp D g t1) (e2 : exp D g t2) : @@ -1146,6 +1313,27 @@ Lemma execD_bernoulli g r (r1 : (r%:num <= 1)%R) : existT _ (cst [the probability _ _ of bernoulli r1]) (measurable_cst _). Proof. exact/execD_evalD/eval_bernoulli. Qed. +Lemma execD_bernoulli_trunc g e : + @execD g _ (exp_bernoulli_trunc e) = + existT _ (bernoulli_trunc \o projT1 (execD e)) (measurableT_comp measurable_bernoulli_trunc (projT2 (execD e))). +Proof. exact/execD_evalD/eval_bernoulli_trunc/evalD_execD. Qed. + +Lemma execD_binomial g n p (p1 : (p%:num <= 1)%R) : + @execD g _ (exp_binomial n p p1) = + existT _ (cst [the probability _ _ of binomial_probability n p1]) (measurable_cst _). +Proof. exact/execD_evalD/eval_binomial. Qed. + +Lemma execD_binomial_trunc g n e : + @execD g _ (exp_binomial_trunc n e) = + existT _ (binomial_probability_trunc n \o projT1 (execD e)) + (measurableT_comp (measurable_binomial_probability_trunc n) (projT2 (execD e))). +Proof. exact/execD_evalD/eval_binomial_trunc/evalD_execD. Qed. + +Lemma execD_uniform g a b ab0 : + @execD g _ (exp_uniform a b ab0) = + existT _ (cst [the probability _ _ of uniform_probability ab0]) (measurable_cst _). +Proof. exact/execD_evalD/eval_uniform. Qed. + Lemma execD_normalize_pt g t (e : exp P g t) : @execD g _ [Normalize e] = existT _ (normalize_pt (execP e) : _ -> pprobability _ _) diff --git a/theories/lang_syntax_examples.v b/theories/lang_syntax_examples.v index 922dd7be2..76b90c12b 100644 --- a/theories/lang_syntax_examples.v +++ b/theories/lang_syntax_examples.v @@ -187,10 +187,11 @@ Import Notations. Context {R : realType}. Lemma exec_normalize_return g x r : - projT1 (@execD _ g _ [Normalize return r:R]) x = \d_r :> probability _ R. + projT1 (@execD _ g _ [Normalize return r:R]) x = + @dirac _ (measurableTypeR R) r _ :> probability _ R. + (* TODO: try to use the notation \d_r *) Proof. -rewrite execD_normalize_pt execP_return execD_real/=. -exact: normalize_kdirac. +by rewrite execD_normalize_pt execP_return execD_real//=; exact: normalize_kdirac. Qed. End trivial_example. @@ -302,12 +303,6 @@ by congr (_ + _)%E; rewrite ?addeA !muleA -?muleDl//; congr (_ * _)%E; congr (_%:E); field. Qed. -Definition sample_add_syntax0 : @exp R _ [::] _ := - [let "x" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in - let "y" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in - let "z" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in - return #{"x"} && #{"y"} && #{"z"}]. - End sample_pair. Section bernoulli_examples. @@ -346,7 +341,7 @@ rewrite !integral_indic//= !iteE/= /mscale/=. rewrite setTI !diracT !mule1. rewrite ger0_norm//. rewrite -EFinD/= eqe ifF; last first. - by apply/negbTE/negP => /orP[/eqP|//]; rewrite /onem; lra. + apply/negbTE/negP => /orP[/eqP|//]; rewrite /onem. lra. rewrite !letin'E/= !iteE/=. rewrite !ge0_integral_mscale//=. rewrite ger0_norm//. @@ -452,6 +447,51 @@ Qed. End bernoulli_examples. +Section binomial_examples. +Context {R : realType}. +Open Scope lang_scope. +Open Scope ring_scope. + +Definition sample_binomial3 : @exp R _ [::] _ := + [let "x" := Sample {exp_binomial 3 (1 / 2)%:nng (p1S 1)} in + return #{"x"}]. + +Lemma exec_sample_binomial3 t U : measurable U -> + execP sample_binomial3 t U = ((1 / 8)%:E * \d_(0%:R : R) U + + (3 / 8)%:E * \d_(1%:R : R) U + + (3 / 8)%:E * \d_(2%:R : R) U + + (1 / 8)%:E * \d_(3%:R : R) U)%E. +Proof. +move=> mU; rewrite /sample_binomial3 execP_letin execP_sample execP_return. +rewrite exp_var'E (execD_var_erefl "x") !execD_binomial/=. +rewrite letin'E ge0_integral_measure_sum//=; last first. + exact: measurable_fun_dirac. +rewrite !big_ord_recl big_ord0 !ge0_integral_mscale//=; [|exact: measurable_fun_dirac..]. +rewrite !integral_dirac// /bump; [|exact: measurable_fun_dirac..]. +rewrite !binS/= !bin0 bin1 bin2 bin_small// addn0. +rewrite expr0 mulr1 mul1r subn0. +rewrite -2!addeA !mul1r. +congr _%:E. +rewrite indicT !mul1r. +congr (_ + _). + congr (_ * _). + by field. +congr (_ + _). + congr (_ * _). + rewrite expr1 /onem. + by field. +congr (_ + _). + congr (_ * _). + rewrite /onem/=. + by field. +rewrite addr0. +congr (_ * _). +rewrite /onem/=. +by field. +Qed. + +End binomial_examples. + Section hard_constraint'. Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). @@ -475,7 +515,7 @@ Proof. apply/eq_sfkernel => x U. rewrite letin'E/= /sample; unlock. rewrite ge0_integral_measure_add//= ge0_integral_mscale//= ge0_integral_mscale//=. -rewrite integral_dirac//= integral_dirac//= !diracT/= !mul1e. +rewrite !integral_dirac//= !diracT/= !mul1e. by rewrite /mscale/= iteE//= iteE//= fail'E mule0 adde0 ger0_norm. Qed. @@ -571,52 +611,21 @@ Local Open Scope lang_scope. Import Notations. Context {R : realType}. -Section tests. - -Local Notation "$ str" := (@exp_var _ _ str%string _ erefl) - (in custom expr at level 1, format "$ str"). - -Definition staton_bus_syntax0_generic (x r u : string) - (rx : infer (r != x)) (Rx : infer (u != x)) - (ur : infer (u != r)) (xr : infer (x != r)) - (xu : infer (x != u)) (ru : infer (r != u)) : @exp R P [::] _ := - [let x := Sample {exp_bernoulli (2 / 7%:R)%:nng p27} in - let r := if #x then return {3}:R else return {10}:R in - let u := Score {exp_poisson 4 [#r]} in - return #x]. - -Fail Definition staton_bus_syntax0_generic' (x r u : string) - (rx : infer (r != x)) (Rx : infer (u != x)) - (ur : infer (u != r)) (xr : infer (x != r)) - (xu : infer (x != u)) (ru : infer (r != u)) : @exp R P [::] _ := - [let x := Sample {exp_bernoulli (2 / 7%:R)%:nng p27} in - let r := if $x then return {3}:R else return {10}:R in - let u := Score {exp_poisson 4 [$r]} in - return $x]. - -Fail Definition staton_bus_syntax0' : @exp R _ [::] _ := - [let "x" := Sample {exp_bernoulli (2 / 7%:R)%:nng p27} in - let "r" := if ${"x"} then return {3}:R else return {10}:R in - let "_" := Score {exp_poisson 4 [${"r"}]} in - return ${"x"}]. - Definition staton_bus_syntax0 : @exp R _ [::] _ := [let "x" := Sample {exp_bernoulli (2 / 7%:R)%:nng p27} in let "r" := if #{"x"} then return {3}:R else return {10}:R in let "_" := Score {exp_poisson 4 [#{"r"}]} in return #{"x"}]. -End tests. - Definition staton_bus_syntax := [Normalize {staton_bus_syntax0}]. Let sample_bern : R.-sfker munit ~> mbool := sample_cst (bernoulli p27). -Let ite_3_10 : R.-sfker mbool * munit ~> (mR R) := - ite macc0of2 (ret k3) (ret k10). +Let ite_3_10 : R.-sfker mbool * munit ~> measurableTypeR R := + ite macc0of2 (@ret _ _ _ (measurableTypeR R) R _ (kr 3)) (@ret _ _ _ (measurableTypeR R) R _ (kr 10)). -Let score_poisson4 : R.-sfker mR R * (mbool * munit) ~> munit := - score (measurableT_comp (measurable_poisson 4) macc0of2). +Let score_poisson4 : R.-sfker measurableTypeR R * (mbool * munit) ~> munit := + score (measurableT_comp (measurable_poisson 4) (@macc0of2 _ _ (measurableTypeR R) _)). Let kstaton_bus' := letin' sample_bern @@ -703,11 +712,11 @@ Definition staton_busA_syntax : exp _ [::] _ := Let sample_bern : R.-sfker munit ~> mbool := sample_cst (bernoulli p27). -Let ite_3_10 : R.-sfker mbool * munit ~> (mR R) := - ite macc0of2 (ret k3) (ret k10). +Let ite_3_10 : R.-sfker mbool * munit ~> measurableTypeR R := + ite macc0of2 (@ret _ _ _ (measurableTypeR R) R _ (kr 3)) (@ret _ _ _ (measurableTypeR R) R _ (kr 10)). -Let score_poisson4 : R.-sfker mR R * (mbool * munit) ~> munit := - score (measurableT_comp (measurable_poisson 4) macc0of3'). +Let score_poisson4 : R.-sfker measurableTypeR R * (mbool * munit) ~> munit := + score (measurableT_comp (measurable_poisson 4) (@macc0of3' _ _ _ (measurableTypeR R) _ _)). (* same as kstaton_bus _ (measurable_poisson 4) but expressed with letin' instead of letin *) @@ -805,6 +814,11 @@ Section letinC. Local Open Scope lang_scope. Variable (R : realType). +Require Import Classical_Prop. (* TODO: mv *) + +Let weak_head g {t1 t2} x (e : @exp R P g t2) (xg : x \notin dom g) := + exp_weak P [::] _ (x, t1) e xg. + Lemma letinC g t1 t2 (e1 : @exp R P g t1) (e2 : exp P g t2) (x y : string) (xy : infer (x != y)) (yx : infer (y != x)) @@ -812,11 +826,11 @@ Lemma letinC g t1 t2 (e1 : @exp R P g t1) (e2 : exp P g t2) forall U, measurable U -> execP [ let x := e1 in - let y := {exp_weak _ [::] _ (x, t1) e2 xg} in + let y := {weak_head e2 xg} in return (#x, #y)] ^~ U = execP [ let y := e2 in - let x := {exp_weak _ [::] _ (y, t2) e1 yg} in + let x := {weak_head e1 yg} in return (#x, #y)] ^~ U. Proof. move=> U mU; apply/funext => z. diff --git a/theories/lang_syntax_examples_wip.v b/theories/lang_syntax_examples_wip.v new file mode 100644 index 000000000..c2fc64319 --- /dev/null +++ b/theories/lang_syntax_examples_wip.v @@ -0,0 +1,329 @@ +Require Import String. +From HB Require Import structures. +From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. +From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. +From mathcomp.classical Require Import functions cardinality fsbigop. +Require Import signed reals ereal topology normedtype sequences esum measure. +Require Import lebesgue_measure numfun lebesgue_integral kernel prob_lang. +Require Import lang_syntax_util lang_syntax. +From mathcomp Require Import ring lra. + +Set Implicit Arguments. +Unset Strict Implicit. +Unset Printing Implicit Defensive. + +Import Order.TTheory GRing.Theory Num.Def Num.Theory. +Import numFieldTopology.Exports. + +Local Open Scope classical_set_scope. +Local Open Scope ring_scope. +(* Local Open Scope ereal_scope. *) + +Section casino_example. +Open Scope ring_scope. +Open Scope lang_scope. +Context (R : realType). +Lemma a01 : 0 < 1 - 0 :> R. Proof. by []. Qed. + +Definition casino0 : exp _ [::] Bool := + [let "p" := Sample {exp_uniform 0 1 a01} in + let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in + let "_" := if #{"a1"} == {5}:R then return TT else Score {0}:R in + let "a2" := Sample {exp_binomial_trunc 3 [#{"p"}]} in + return {1}:R <= #{"a2"}]. + +Example e1 : @exp R _ [::] _ := [{1}:R + {2}:R * {2}:R ^+ {3%nat}]. + +Lemma exe1 : projT1 (execD e1) = (fun x => 17). +Proof. +rewrite /e1 (@execD_bin _ _ binop_add) (@execD_bin _ _ binop_mult)/=. +rewrite execD_pow/= !execD_real /=. +apply: funext => x /=. +lra. +Qed. + +(* Arguments exp_bin {R g b} &. *) +Definition casino1 : @exp R _ [::] _ := + [let "p" := Sample {exp_uniform 0 1 a01} in + let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in + let "_" := if #{"a1"} == {5}:R then return TT else Score {0}:R in + let "a2" := Sample + {exp_bernoulli_trunc [{1}:R - #{"p"} ^+ {3%nat}]} in + return #{"a2"}]. + +(* exec [let x := sample (binomial n e) in *) +(* return (x >= 1)] = *) +(* exec [let y := sample (bernoulli (1 - e^n)) in *) +(* return y] *) +Lemma binomial_le1 x y n g e t U : + (0 <= (projT1 (execD e) t : mtyp Real) <= 1) -> + execP ([let x := Sample {exp_binomial_trunc n e} in + return {1}:R <= #x] : @exp R _ g _) t U = + execP [let y := Sample {exp_bernoulli_trunc [{1}:R - e ^+ n]} in + return #y] t U. +Proof. +rewrite !execP_letin !execP_sample execD_binomial_trunc execD_bernoulli_trunc/=. +rewrite !exp_var'E/=. + exact/ctx_prf_head. + exact/ctx_prf_head. +move=> H0 H1. +rewrite !execP_return !execD_rel. + have /= := @execD_var R ((y, Bool) :: g) y. + rewrite eqxx => /(_ H0) ->. + have /= := @execD_var R ((x, Real) :: g) x. + rewrite eqxx => /(_ H1) -> /=. +rewrite (@execD_bin _ _ binop_minus) execD_pow !execD_real/=. +rewrite 2!letin'E/=. +set p := projT1 (execD e) t. +move => /andP[p0 p1]. +rewrite (@integral_bernoulli_trunc _ _ (fun x => \d_x U))//; last first. + apply/andP; split. + exact: (onemX_ge0 _ p0 p1). + apply/onem_le1/exprn_ge0/p0. +rewrite (@integral_binomial_probabilty_trunc _ n p _ _ (fun y => \d_(1 <= y)%R U))//; last first. + (* move=>/= _ y0 my0. + rewrite setTI. *) + apply: measurable_fun_dirac. + have := @subsetT _ U; rewrite setT_bool => UT. + have [->|->|->|->] /= := subset_set2 UT. + exact: measurable0. + rewrite [X in measurable X](_ : _ = `[1, +oo[%classic) //. + apply/seteqP. + admit. + rewrite [X in measurable X](_ : _ = `]-oo, 1[%classic) //. + apply/seteqP. + admit. + admit. + (* rewrite diracE. *) +rewrite !big_ord_recl/=. +have -> : (1 <= 0 :> R) = false by lra. +rewrite /bump. +under eq_bigr => i _. + rewrite /= natrD. + have -> : 1 <= 1 + i%:R :> R. + by rewrite lerDl. + over. +rewrite addeC -ge0_sume_distrl. + congr (_ + _)%E; congr (_ * _)%E. + have -> : (\sum_(i < n) (p ^+ (n - (1 + i)) * `1-p ^+ (1 + i) *+ 'C(n, 1 + i))%:E)%E = + (\sum_(i < n.+1) (p ^+ (n - i) * `1-p ^+ i *+ 'C(n, i))%:E - (p ^+ n)%:E)%E. + rewrite big_ord_recl/= subn0 addeC addeA. + rewrite bin0 mulr1 mulr1n. + have <- : 0%E = ((- p ^+ n)%:E + (p ^+ n)%:E)%E. + rewrite EFinN. + congr _%:E. + lra. + by rewrite add0e. + congr _%E. + rewrite sumEFin. + rewrite !EFinB EFin_expe. + congr (_ - _)%E. + rewrite -(@exprDn_comm _ p `1-p n); last first. + by rewrite /GRing.comm/onem; lra. + rewrite /onem addrC. + have -> : 1 - p + p = 1 by lra. + by rewrite expr1n. + rewrite subn0 expr0 bin0 mulr1 mulr1n. + rewrite /onem. + congr _%:E. + set pn := p ^+ n. + lra. +move=> i _. +apply/mulrn_wge0/mulr_ge0; apply/exprn_ge0. +exact: p0. +apply/onem_ge0/p1. +Admitted. + +Lemma __ : uniform_probability a01 `[0, (1 / 2)] = (1 / 2)%:E. +Proof. +rewrite /uniform_probability /mscale/= /mrestr. +Abort. + +Lemma letin'_sample_uniform d d' (T : measurableType d) + (T' : measurableType d') (a b : R) (ab0 : (0 < b - a)%R) + (u : R.-sfker [the measurableType _ of (_ * T)%type] ~> T') x y : + letin' (sample_cst (uniform_probability ab0)) u x y = + (((b - a)^-1)%:E * \int[lebesgue_measure]_(x0 in `[a, b]) u (x0, x) y)%E. +Proof. +rewrite letin'E/=. +rewrite ge0_integral_mscale//=; last admit. +transitivity ( +(((b - a)^-1)%:E * \int[lebesgue_measure]_(x0 in `[a, b]) u (x0, x) y)%E +). + admit. +by []. +Admitted. + +Lemma execP_letin_uniform g u a b p (Hap : infer (a != p)) +(Hbp : infer (b != p)) (s0 s1 : @exp R D ((a, Unit) :: (b, Real) :: (p, Real) :: g) Real -> exp P ((p, Real) :: g) u) : + (forall (t : R) x U, t \in `[0, 1] -> execP (s0 [#p]) (t, x) U = execP (s1 [#p]) (t, x) U) -> + execP [let p := Sample {@exp_uniform _ g 0 1 a01} in {s0 [#p]}] = + execP [let p := Sample {@exp_uniform _ g 0 1 a01} in {s1 [#p]}]. +Proof. +move=> s01. +rewrite !execP_letin execP_sample execD_uniform/=. +apply: eq_sfkernel => x U. +rewrite 2!letin'_sample_uniform. +congr (_ * _)%E. +apply: eq_integral => t t01. +apply: s01. +by rewrite inE in t01. +Qed. + +Lemma casino01 : execP casino0 = execP casino1. +Proof. +rewrite /casino0 /casino1. +pose s0 := fun x => [let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} + in let "_" := if #{"a1"} == {5}:R then return TT else Score {0}:R + in let "a2" := Sample {exp_binomial_trunc 3 [x]} in {exp_return [{1}:R <= #{"a2"}]}] : @exp R _ _ _. +pose s1 := fun x => [let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} + in let "_" := if #{"a1"} == {5}:R then return TT else Score {0}:R + in let "a2" + := Sample {exp_bernoulli_trunc [{1}:R - x ^+ {3%N}]} + in {exp_return [#{"a2"}]}] : @exp R _ _ _. +apply: (@execP_letin_uniform [::] _ _ _ _ _ _ (s0 _) (s1 _)). +move=> e k k01 ek. +rewrite /s0/s1. +rewrite 2!execP_letin. +rewrite 2![in RHS]execP_letin. +congr (letin' _ _ _ _). +congr (letin' _ _ _). +(* apply: eq_sfkernel => x U. *) +rewrite !execP_letin !execP_sample execD_bernoulli_trunc execD_binomial_trunc/=. +rewrite (@execD_bin _ _ binop_minus) !execP_return execD_rel execD_pow/=. +rewrite !execD_real !exp_var'E !(execD_var_erefl "a2") (execD_var_erefl "p")/=. + +have H := (@binomial_le1 "a2" "a2" 3 _ [#{"p"}]). +(* f_equal. *) +(* congr ((letin' _ _ _) _). +apply: eq_sfkernel => ?. +have : 0 <= e <= 1. + admit. +move=> H. +rewrite (binomial_le1 _ _ _ _ H). +under eq_integral. +rewrite execP_letin. +congr (letin'). + +(* apply: eq_sfkernel => x U. *) +have : 0 <= (projT1 (execD e) x : mtyp Real) <= 1. + by rewrite ek. +move=> H. +apply: (binomial_le1 _ _ _ _ H). *) +Admitted. + +(* guard test *) +Definition test_guard : @exp R _ [::] _ := [ + let "p" := Sample {exp_bernoulli (1 / 3)%:nng (p1S 2)} in + let "_" := if #{"p"} then return TT else Score {0}:R in + return #{"p"} +]. + +Lemma exec_guard t U : execP test_guard t U = ((1 / 3)%:E * @dirac _ _ true R U)%E. +Proof. +rewrite /test_guard 2!execP_letin execP_sample execD_bernoulli execP_if/=. +rewrite !execP_return !exp_var'E !(execD_var_erefl "p") execD_unit execP_score execD_real/=. +rewrite letin'E ge0_integral_measure_sum//. +rewrite !big_ord_recl big_ord0 !ge0_integral_mscale//= !integral_dirac//. +rewrite !letin'E !iteE/= integral_dirac// ge0_integral_mscale//=. +by rewrite normr0 mul0e !mule0 !adde0 !diracT !mul1e. +Qed. + +Lemma exec_casino t U : + execP casino0 t U = ((10 / 99)%:E * @dirac _ _ true R U + + (1 / 99)%:E * @dirac _ _ false R U)%E . +Proof. +rewrite /casino0 !execP_letin !execP_sample execD_uniform/=. +rewrite !execD_binomial_trunc execP_if !execP_return !execP_score/=. +rewrite !execD_rel !execD_real execD_unit/=. +rewrite !exp_var'E (execD_var_erefl "p") (execD_var_erefl "a1"). +rewrite (execD_var_erefl "p") (execD_var_erefl "a2")/=. +rewrite letin'E/= /uniform_probability ge0_integral_mscale//=. +rewrite subr0 invr1 mul1e. +under eq_integral. + +Admitted. + +Definition uniform_syntax : @exp R _ [::] _ := + [let "p" := Sample {exp_uniform 0 1 a01} in + return #{"p"}]. + +About integralT_nnsfun. + +Lemma exec_uniform_syntax t U : measurable U -> + execP uniform_syntax t U = uniform_probability a01 U. +Proof. +move=> mU. +rewrite /uniform_syntax execP_letin execP_sample execP_return !execD_uniform. +rewrite exp_var'E (execD_var_erefl "p")/=. +rewrite letin'E /=. +rewrite {1}/uniform_probability. +set x := (X in mscale _ X). +set k := (X in mscale X _). +transitivity ((k%:num)%:E * \int[x]_y \d_y U)%E. + rewrite -ge0_integral_mscale //. + exact: measurable_fun_dirac. +rewrite /uniform_probability /mscale /=. +congr (_ * _)%E. +under eq_integral do rewrite diracE. +rewrite /=. +rewrite /lebesgue_measure/=. +rewrite -integral_cst. +set f := (mrestr lebesgue_measure (measurable_itv `[0%R, 1%R])). +admit. +rewrite -(@ge0_integral_mscale _ _ _ x setT measurableT k (fun y => \d_y U)) //. +rewrite /uniform_probability/mscale/=/integral//=. +Admitted. + +Definition binomial_le : @exp R _ [::] Bool := + [let "a2" := Sample {exp_binomial 3 (1 / 2)%:nng (p1S 1)} in + return {1}:R <= #{"a2"}]. + +Lemma exec_binomial_le t U : + execP binomial_le t U = ((7 / 8)%:E * @dirac _ _ true R U + + (1 / 8)%:E * @dirac _ _ false R U)%E. +Proof. +rewrite /binomial_le execP_letin execP_sample execP_return execD_rel execD_real. +rewrite exp_var'E (execD_var_erefl "a2") execD_binomial. +rewrite letin'E//= /binomial_probability ge0_integral_measure_sum//=. +rewrite !big_ord_recl big_ord0 !ge0_integral_mscale//=. +rewrite !integral_dirac// /bump. +rewrite !binS/= !bin0 bin1 bin2 bin_small// addn0. +rewrite addeC adde0. +congr (_ + _)%:E. +have -> : (1 <= 1)%R. admit. +have -> : (1 <= 2)%R. admit. +have -> : (1 <= 3)%R. admit. +rewrite -!mulrDl indicT !mul1r. +congr (_ * _). +rewrite /onem addn0 add0n. +by field. +congr (_ * _). +by field. +(* by rewrite ler10. *) +Admitted. + +Definition binomial_guard : @exp R _ [::] Real := + [let "a1" := Sample {exp_binomial 3 (1 / 2)%:nng (p1S 1)} in + let "_" := if #{"a1"} == {1}:R then return TT else Score {0}:R in + return #{"a1"}]. + +Lemma exec_binomial_guard t U : + execP binomial_guard t U = ((7 / 8)%:E * @dirac _ R 1%R R U + + (1 / 8)%:E * @dirac _ R 0%R R U)%E. +Proof. +rewrite /binomial_guard !execP_letin execP_sample execP_return execP_if. +rewrite !exp_var'E execD_rel !(execD_var_erefl "a1") execP_return execD_unit execD_binomial execD_real execP_score execD_real. +rewrite !letin'E//= /binomial_probability ge0_integral_measure_sum//=. +rewrite !big_ord_recl big_ord0 !ge0_integral_mscale//=. +rewrite !integral_dirac// /bump. +rewrite (* indicT *) !binS/= !bin0 bin1 bin2 bin_small// addn0 (* !mul1e *). +rewrite !letin'E//= !iteE/= !diracE/=. +have -> : (0 == 1)%R = false; first by admit. +have -> : (1 == 1)%R; first by admit. +have -> : (2 == 1)%R = false; first by admit. +have -> : (3 == 1)%R = false; first by admit. +rewrite addeC adde0. +Admitted. + +End casino_example. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 9499d9317..d0fa17d55 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -3,11 +3,11 @@ From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval finmap. From mathcomp Require Import rat archimedean. From mathcomp Require Import mathcomp_extra boolp classical_sets. -From mathcomp.classical Require Import functions cardinality fsbigop. +From mathcomp Require Import functions cardinality fsbigop. From mathcomp Require Import reals ereal signed topology normedtype sequences. From mathcomp Require Import esum measure lebesgue_measure numfun. From mathcomp Require Import lebesgue_integral exp kernel. -From mathcomp Require Import lra. +From mathcomp Require Import ring lra. (******************************************************************************) (* Semantics of a probabilistic programming language using s-finite kernels *) @@ -43,6 +43,26 @@ Local Open Scope classical_set_scope. Local Open Scope ring_scope. Local Open Scope ereal_scope. +(* Definition mR (R : realType) : Type := R. +HB.instance Definition _ (R : realType) := Measurable.on (mR R). +(* [the measurableType (R.-ocitv.-measurable).-sigma of + salgebraType (R.-ocitv.-measurable)]. *) *) + +Module Notations. +(*Notation var1of2 := (@measurable_fst _ _ _ _). +Notation var2of2 := (@measurable_snd _ _ _ _). +Notation var1of3 := (measurableT_comp (@measurable_fst _ _ _ _) + (@measurable_fst _ _ _ _)). +Notation var2of3 := (measurableT_comp (@measurable_snd _ _ _ _) + (@measurable_fst _ _ _ _)). +Notation var3of3 := (@measurable_snd _ _ _ _).*) + +(* Definition mR R := [the measurableType (R.-ocitv.-measurable).-sigma of + salgebraType (R.-ocitv.-measurable)]. *) +Notation munit := Datatypes_unit__canonical__measure_Measurable. +Notation mbool := Datatypes_bool__canonical__measure_Measurable. +End Notations. + (* TODO: PR *) Lemma onem_nonneg_proof (R : numDomainType) (p : {nonneg R}) : (p%:num <= 1 -> 0 <= `1-(p%:num))%R. @@ -86,6 +106,34 @@ subst p2. by f_equal. Qed. +Section constants. +Variable R : realType. +Local Open Scope ring_scope. + +Lemma onem1S n : `1- (1 / n.+1%:R) = (n%:R / n.+1%:R)%:nng%:num :> R. +Proof. +by rewrite /onem/= -{1}(@divrr _ n.+1%:R) ?unitfE// -mulrBl -natr1 addrK. +Qed. + +Lemma p1S n : (1 / n.+1%:R)%:nng%:num <= 1 :> R. +Proof. by rewrite ler_pdivrMr//= mul1r ler1n. Qed. + +Lemma p12 : (1 / 2%:R)%:nng%:num <= 1 :> R. Proof. by rewrite p1S. Qed. + +Lemma p14 : (1 / 4%:R)%:nng%:num <= 1 :> R. Proof. by rewrite p1S. Qed. + +Lemma onem27 : `1- (2 / 7%:R) = (5%:R / 7%:R)%:nng%:num :> R. +Proof. by apply/eqP; rewrite subr_eq/= -mulrDl -natrD divrr// unitfE. Qed. + +Lemma p27 : (2 / 7%:R)%:nng%:num <= 1 :> R. +Proof. by rewrite /= lter_pdivrMr// mul1r ler_nat. Qed. + +End constants. +Arguments p12 {R}. +Arguments p14 {R}. +Arguments p27 {R}. +Arguments p1S {R}. + Section bernoulli. Variables (R : realType) (p : {nonneg R}) (p1 : (p%:num <= 1)%R). @@ -107,27 +155,230 @@ HB.instance Definition _ := End bernoulli. -Lemma integral_bernoulli {R : realType} - (p : {nonneg R}) (p1 : (p%:num <= 1)%R) (f : bool -> set bool -> _) U : - (forall x y, 0 <= f x y) -> - \int[bernoulli p1]_y (f y ) U = - p%:num%:E * f true U + (`1-(p%:num))%:E * f false U. +Lemma integral_bernoulli {R : realType} (p : {nonneg R}) (p1 : (p%:num <= 1)%R) + (f : bool -> \bar R) : (forall x, 0 <= f x) -> + \int[bernoulli p1]_y (f y) = + p%:num%:E * f true + (`1-(p%:num))%:E * f false. Proof. move=> f0. rewrite ge0_integral_measure_sum// 2!big_ord_recl/= big_ord0 adde0/=. by rewrite !ge0_integral_mscale//= !integral_dirac//= !diracT 2!mul1e. Qed. -Section uniform_probability. +Section bernoulli_trunc. +Variables (R : realType). +Local Open Scope ring_scope. + +Lemma sumbool_ler (x y : R) : {(x <= y)%R} + {(x > y)%R}. +Proof. +have [_|_] := leP x y. +by apply left. +by apply right. +Qed. + +Definition bernoulli0 := @bernoulli R 0%R%:nng ler01. + +HB.instance Definition _ := Probability.on bernoulli0. + +Definition bernoulli_trunc (p : R) := match (sumbool_ler 0%R p) with +| left l0p => match (sumbool_ler (NngNum l0p)%:num 1%R) with + | left lp1 => [the probability _ _ of @bernoulli R (NngNum l0p) lp1] + | right _ => [the probability _ _ of bernoulli0] + end +| right _ => [the probability _ _ of bernoulli0] +end. + +HB.instance Definition _ (p : R) := Probability.on (bernoulli_trunc p). + +Lemma measurable_bernoulli_trunc : measurable_fun setT (bernoulli_trunc : _ -> pprobability _ _). +Proof. +(* move=> _ Y mY. *) +(* rewrite setTI. *) +apply: (@measurability _ _ _ _ _ _ + (@pset _ _ _ : set (set (pprobability _ R)))) => //. +move=> _ -[_ [r r01] [Ys mYs <-]] <-. +apply: emeasurable_fun_infty_o => //. +rewrite /bernoulli_trunc. +(* apply: measurable_fun_case_suml. +case: sumbool_ler. *) +(* case: (sumbool_ler 0 p) => [p0'/=|]. + case: (sumbool_ler p 1) => [p1'/=|]. *) +(* exact: (measurable_kernel (knormalize f point) Ys). *) +Admitted. + +End bernoulli_trunc. + +Arguments bernoulli_trunc {R}. +Arguments measurable_bernoulli_trunc {R}. + +Lemma integral_bernoulli_trunc {R : realType} (p : R) (f : bool -> \bar R) : + (0 <= p <= 1)%R -> + (forall x, 0 <= f x) -> + \int[bernoulli_trunc p]_y (f y) = + p%:E * f true + (`1-p)%:E * f false. +Proof. +move=> /andP[p0 p1] f0. +rewrite /bernoulli_trunc. +case: (sumbool_ler 0 p) => [p0'/=|]. + case: (sumbool_ler p 1) => [p1'/=|]. + by rewrite integral_bernoulli. + by rewrite ltNge p1. +by rewrite ltNge p0. +Qed. + +Section binomial_probability. +Context {R : realType} (n : nat) (p : {nonneg R}) (p1 : (p%:num <= 1)%R). +Local Open Scope ring_scope. + +(* C(n, k) * p^(n-k) * (1-p)^k *) +Definition bino_term (k : nat) :{nonneg R} := + (p%:num^+(n-k)%N * (NngNum (onem_ge0 p1))%:num^+k *+ 'C(n, k))%:nng. + +Lemma bino_term0 : + bino_term 0 = (p%:num^+n)%:nng. +Proof. +rewrite /bino_term bin0 subn0/=. +apply/val_inj => /=. +by field. +Qed. + +Lemma bino_term1 : + bino_term 1 = (p%:num^+(n-1)%N * (NngNum (onem_ge0 p1))%:num *+ n)%:nng. +Proof. +rewrite /bino_term bin1/=. +apply/val_inj => /=. +by rewrite expr1. +Qed. + +(* \sum_(k < n.+1) (bino_coef p n k) * \d_k. *) +Definition binomial_probability : set (measurableTypeR R) -> \bar R := + @msum _ _ R + (fun k => [the measure _ _ of mscale (bino_term k) + [the measure _ _ of @dirac _ (measurableTypeR R) k%:R R]]) n.+1. + +HB.instance Definition _ := Measure.on binomial_probability. + +Let binomial_setT : binomial_probability [set: _] = 1%:E. +Proof. +rewrite /binomial_probability/msum/mscale/bino_term/=/mscale/=. +under eq_bigr do rewrite diracT mule1. +rewrite sumEFin. +rewrite -exprDn_comm; last by rewrite /GRing.comm mulrC. +by rewrite add_onemK; congr _%:E; rewrite expr1n. +Qed. + +HB.instance Definition _ := + @Measure_isProbability.Build _ _ R binomial_probability binomial_setT. + +End binomial_probability. + +Section integral_binomial. +Variables (R : realType) (d : measure_display) (T : measurableType d). + +Lemma integral_binomial (n : nat) (p : {nonneg R}) + (p1 : (p%:num <= 1)%R) (f : R -> \bar R) + (mf : measurable_fun setT f) : + (forall x, 0 <= f x) -> \int[binomial_probability n p1]_y (f y) = + \sum_(k < n.+1) (bino_term n p1 k)%:num%:E * f k%:R. +Proof. +move=> f0. +rewrite ge0_integral_measure_sum//=; last first. +apply: eq_bigr => i _. +rewrite ge0_integral_mscale//=; last first. +rewrite integral_dirac//=; last first. +by rewrite diracT mul1e. +Qed. + +End integral_binomial. + +Section binomial_trunc. +Variables (R : realType). +Local Open Scope ring_scope. + +Definition binomial_probability0 := @binomial_probability R 0 0%:nng%R ler01. + +Definition binomial_probability_trunc n (p : R) := + (* (p1 : (p%:num <= 1)%R) := *) + match (sumbool_ler 0%R p) with + | left l0p => match (sumbool_ler (NngNum l0p)%:num 1%R) with + | left lp1 => [the probability _ _ of @binomial_probability R n (NngNum l0p) lp1] + | right _ => [the probability _ _ of binomial_probability0] + end + | right _ => [the probability _ _ of binomial_probability0] + end. + +Lemma measurable_binomial_probability_trunc (n : nat) + : measurable_fun setT (binomial_probability_trunc n : R -> pprobability _ _). +Admitted. + +End binomial_trunc. + +Arguments binomial_probability_trunc {R}. +Arguments measurable_binomial_probability_trunc {R}. + +Section integral_binomial_trunc. +Variables (R : realType) (d : measure_display) (T : measurableType d). + +Lemma integral_binomial_probabilty_trunc (n : nat) (p : R) + (p0 : (0 <= p)%R) (p1 : ((NngNum p0)%:num <= 1)%R) (f : R -> \bar R) (mf : measurable_fun setT f) : + (forall x, 0 <= f x) -> \int[binomial_probability_trunc n p]_y (f y) = \sum_(k < n.+1) (bino_term n p1 k)%:num%:E * f k%:R. +Proof. +move=> f0. +rewrite /binomial_probability_trunc/=. +case: (sumbool_ler 0 p) => [p0'/=|]. + case: (sumbool_ler p 1) => [p1'/=|]. + by rewrite integral_binomial. + by rewrite ltNge p1. +by rewrite ltNge p0. +Qed. + +End integral_binomial_trunc. + +Section binomial_example. Context {R : realType}. -Variables (a b : R) (ab0 : (0 < b - a)%R). +Open Scope ring_scope. + +Lemma binomial3_2 : @binomial_probability R 3 _ (p1S 1) [set 2%:R] = (3 / 8)%:E. +Proof. +rewrite /binomial_probability/msum !big_ord_recl/= big_ord0 adde0 bino_term0. +rewrite /mscale/= !diracE /bump/=. +repeat rewrite ?binS ?bin0 ?bin1 ?bin_small//. +rewrite memNset//=; last by move/eqP; rewrite eqr_nat. +rewrite memNset//=; last by move/eqP; rewrite eqr_nat. +rewrite mem_set//=. +rewrite memNset//=; last by move/eqP; rewrite eqr_nat. +congr _%:E. +rewrite expr0 !mulr1 !mulr0 !add0r !addn0 !add0n /onem. +by field. +Qed. -Definition uniform_probability := mscale (invr_nonneg (NngNum (ltW ab0))) - (mrestr lebesgue_measure (measurable_itv `[a, b])). +End binomial_example. + +Section uniform_probability. +Context (R : realType) (a b : R) (ab0 : (0 < b - a)%R). +Definition uniform_probability +(* : set _ -> \bar R *) + := @mscale _ _ R (invr_nonneg (NngNum (ltW ab0))) + (mrestr lebesgue_measure (measurable_itv `[a, b])). + +(** TODO: set R -> \bar R を書くとMeasure.onが通らない **) +(** **) HB.instance Definition _ := Measure.on uniform_probability. -Let uniform_probability_setT : uniform_probability [set: _] = 1. +(* Let uniform0 : uniform_probability set0 = 0. +Proof. exact: measure0. Qed. + +Let uniform_ge0 U : 0 <= uniform_probability U. +Proof. exact: measure_ge0. Qed. + +Let uniform_sigma_additive : semi_sigma_additive uniform_probability. +Proof. move=> /= F mF tF mUF; exact: measure_semi_sigma_additive. Qed. + +HB.instance Definition _ := isMeasure.Build _ _ _ uniform_probability + uniform0 uniform_ge0 uniform_sigma_additive. *) + +Let uniform_probability_setT : uniform_probability [set: _] = 1%:E. Proof. rewrite /uniform_probability /mscale/= /mrestr/=. rewrite setTI lebesgue_measure_itv/= lte_fin. @@ -614,22 +865,6 @@ Proof. by rewrite /fail letinE ge0_integral_mscale//= normr0 mul0e. Qed. End hard_constraint. Arguments fail {d d' X Y R}. -Module Notations. - -(*Notation var1of2 := (@measurable_fst _ _ _ _). -Notation var2of2 := (@measurable_snd _ _ _ _). -Notation var1of3 := (measurableT_comp (@measurable_fst _ _ _ _) - (@measurable_fst _ _ _ _)). -Notation var2of3 := (measurableT_comp (@measurable_snd _ _ _ _) - (@measurable_fst _ _ _ _)). -Notation var3of3 := (@measurable_snd _ _ _ _).*) - -Notation mR := Real_sort__canonical__measure_Measurable. -Notation munit := Datatypes_unit__canonical__measure_Measurable. -Notation mbool := Datatypes_bool__canonical__measure_Measurable. - -End Notations. - Section cst_fun. Context d (T : measurableType d) (R : realType). @@ -1048,35 +1283,6 @@ Qed. End letinC. (* sample programs *) - -Section constants. -Variable R : realType. -Local Close Scope ereal_scope. - -Lemma onem1S n : `1- (1 / n.+1%:R) = (n%:R / n.+1%:R)%:nng%:num :> R. -Proof. -by rewrite /onem/= -{1}(@divrr _ n.+1%:R) ?unitfE// -mulrBl -natr1 addrK. -Qed. - -Lemma p1S n : (1 / n.+1%:R)%:nng%:num <= 1 :> R. -Proof. by rewrite ler_pdivrMr//= mul1r ler1n. Qed. - -Lemma p12 : (1 / 2%:R)%:nng%:num <= 1 :> R. Proof. by rewrite p1S. Qed. - -Lemma p14 : (1 / 4%:R)%:nng%:num <= 1 :> R. Proof. by rewrite p1S. Qed. - -Lemma onem27 : `1- (2 / 7%:R) = (5%:R / 7%:R)%:nng%:num :> R. -Proof. by apply/eqP; rewrite subr_eq/= -mulrDl -natrD divrr// unitfE. Qed. - -Lemma p27 : (2 / 7%:R)%:nng%:num <= 1 :> R. -Proof. by rewrite /= lter_pdivrMr// mul1r ler_nat. Qed. - -End constants. -Arguments p12 {R}. -Arguments p14 {R}. -Arguments p27 {R}. -Arguments p1S {R}. - Section poisson. Variable R : realType. Local Open Scope ring_scope. @@ -1168,10 +1374,10 @@ Context d (T : measurableType d) (R : realType). let r = case x of {(1, _) => return (k3()), (2, _) => return (k10())} in return r *) -Definition sample_and_branch : R.-sfker T ~> mR R := +Definition sample_and_branch : R.-sfker T ~> _ := letin (sample_cst [the probability _ _ of bernoulli p27]) (* T -> B *) - (ite macc1of2 (ret k3) (ret k10)). + (ite macc1of2 (ret (@k3 _ _ R)) (ret k10)). Lemma sample_and_branchE t U : sample_and_branch t U = (2 / 7%:R)%:E * \d_(3%:R : R) U + @@ -1190,6 +1396,28 @@ Definition bernoulli_and : R.-sfker T ~> mbool := (letin (sample_cst [the probability _ _ of bernoulli p12]) (letin (sample_cst [the probability _ _ of bernoulli p12]) (ret (measurable_and macc1of3 macc2of3)))). +(* +Definition mand (x y : T * mbool * mbool -> mbool) + (t : T * mbool * mbool) : mbool := x t && y t. + +Lemma measurable_fun_mand (x y : T * mbool * mbool -> mbool) : + measurable_fun setT x -> measurable_fun setT y -> + measurable_fun setT (mand x y). +Proof. +move=> /= mx my; apply: (measurable_fun_bool true). +rewrite [X in measurable X](_ : _ = + (x @^-1` [set true]) `&` (y @^-1` [set true])); last first. + by rewrite /mand; apply/seteqP; split => z/= /andP. +apply: measurableI. +- by rewrite -[X in measurable X]setTI; exact: mx. +- by rewrite -[X in measurable X]setTI; exact: my. +Qed. + +Definition bernoulli_and : R.-sfker T ~> mbool := + (letin (sample_cst [the probability _ _ of bernoulli p12]) + (letin (sample_cst [the probability _ _ of bernoulli p12]) + (ret (measurable_fun_mand macc1of3 macc2of3)))). +*) Lemma bernoulli_andE t U : bernoulli_and t U = @@ -1231,7 +1459,7 @@ Context d (T : measurableType d) (R : realType). Let poisson4 := @poisson R 4%N. Let mpoisson4 := @measurable_poisson R 4%N. -Definition kstaton_bus_poisson : R.-sfker (mR R) ~> mbool := +Definition kstaton_bus_poisson : R.-sfker R ~> mbool := kstaton_bus _ mpoisson4. Let kstaton_bus_poissonE t U : kstaton_bus_poisson t U = @@ -1262,7 +1490,7 @@ Lemma staton_busE P (t : R) U : ((2 / 7%:R)%:E * (poisson4 3%:R)%:E * \d_true U + (5%:R / 7%:R)%:E * (poisson4 10%:R)%:E * \d_false U) * N^-1%:E. Proof. -rewrite /staton_bus normalizeE /= !kstaton_bus_poissonE !diracT !mule1 ifF //. +rewrite /staton_bus normalizeE !kstaton_bus_poissonE !diracT !mule1 ifF //. apply/negbTE; rewrite gt_eqF// lte_fin. by rewrite addr_gt0// mulr_gt0//= ?divr_gt0// ?ltr0n// poisson_gt0// ltr0n. Qed. @@ -1281,7 +1509,7 @@ Let mexp1560 := @mexp_density R (ratr (15%:Q / 60%:Q)). (* 15/60 = 0.25 *) -Definition kstaton_bus_exponential : R.-sfker (mR R) ~> mbool := +Definition kstaton_bus_exponential : R.-sfker R ~> mbool := kstaton_bus _ mexp1560. Let kstaton_bus_exponentialE t U : kstaton_bus_exponential t U = @@ -1883,7 +2111,7 @@ Definition von_neumann_trick' : R.-sfker (T * unit) ~> (unit + bool)%type := letin (sample_cst D) (letin (sample_cst D) (letin (lift_neq macc1of3 macc2of3) - (ite (macc3of4) + (ite macc3of4 (letin (ret macc1of4) (ret minlb)) (ret kinrtt)))). diff --git a/theories/prob_lang_wip.v b/theories/prob_lang_wip.v index f07c4abf4..4e03fbc74 100644 --- a/theories/prob_lang_wip.v +++ b/theories/prob_lang_wip.v @@ -124,7 +124,7 @@ apply: (measurable_comp (F := [set r : R | r != 0%R])) => //. - exact: measurable_fun_gauss_density. Qed. -Variable mu : {measure set mR R -> \bar R}. +Variable mu : {measure set R -> \bar R}. Definition staton_lebesgue : R.-sfker T ~> _ := letin (sample_cst (gauss01 integral_gauss01_density : pprobability _ _)) From ffd9c3d748a8e46c710d95746dd22b072bdfe012 Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Tue, 9 Jan 2024 19:29:56 +0900 Subject: [PATCH 12/36] bernoulli_trunc measurable --- theories/prob_lang.v | 84 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 14 deletions(-) diff --git a/theories/prob_lang.v b/theories/prob_lang.v index d0fa17d55..a75d4fa4b 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -176,35 +176,91 @@ by apply left. by apply right. Qed. +(* TODO: move? *) +Definition dep_uncurry := +(fun (A : Type) (B : A -> Type) (C : Type) (f : forall a : A, B a -> C) (p : {a : A & B a}) => let (a, Ba) := p in f a Ba) : +forall [A : Type] [B : A -> Type] [C : Type], +(forall a : A, B a -> C) -> {a : A & B a} -> C. + Definition bernoulli0 := @bernoulli R 0%R%:nng ler01. HB.instance Definition _ := Probability.on bernoulli0. -Definition bernoulli_trunc (p : R) := match (sumbool_ler 0%R p) with -| left l0p => match (sumbool_ler (NngNum l0p)%:num 1%R) with +Definition bernoulli_trunc (p : R) := match sumbool_ler 0%R p with +| left l0p => match sumbool_ler (NngNum l0p)%:num 1%R with | left lp1 => [the probability _ _ of @bernoulli R (NngNum l0p) lp1] | right _ => [the probability _ _ of bernoulli0] end | right _ => [the probability _ _ of bernoulli0] end. -HB.instance Definition _ (p : R) := Probability.on (bernoulli_trunc p). +(*HB.instance Definition _ (p : R) := Probability.on (bernoulli_trunc p).*) + +Let simpe := (@mule0 R, @adde0 R, @mule1 R, @add0e R). -Lemma measurable_bernoulli_trunc : measurable_fun setT (bernoulli_trunc : _ -> pprobability _ _). +Lemma measurable_bernoulli_trunc : + measurable_fun setT (bernoulli_trunc : _ -> pprobability _ _). Proof. -(* move=> _ Y mY. *) -(* rewrite setTI. *) apply: (@measurability _ _ _ _ _ _ (@pset _ _ _ : set (set (pprobability _ R)))) => //. move=> _ -[_ [r r01] [Ys mYs <-]] <-. -apply: emeasurable_fun_infty_o => //. -rewrite /bernoulli_trunc. -(* apply: measurable_fun_case_suml. -case: sumbool_ler. *) -(* case: (sumbool_ler 0 p) => [p0'/=|]. - case: (sumbool_ler p 1) => [p1'/=|]. *) -(* exact: (measurable_kernel (knormalize f point) Ys). *) -Admitted. +apply: emeasurable_fun_infty_o => //=. +rewrite /bernoulli_trunc/=. +have := @subsetT _ Ys; rewrite setT_bool => UT. +have [->|->|->|->] /= := subset_set2 UT. +- rewrite [X in measurable_fun _ X](_ : _ = (fun=> 0%E))//. + apply/funext => x/=. + by case: sumbool_ler. +- rewrite [X in measurable_fun _ X](_ : _ = (fun x => if 0 <= x <= 1 then x%:E else 0%E))//. + apply: measurable_fun_ifT => //=; apply: measurable_and => //; apply: (measurable_fun_bool true) => //=. + rewrite (_ : _ @^-1` _ = `[0, +oo[%classic)//. + by apply/seteqP; split => [x|x] /=; rewrite in_itv/= andbT. + by rewrite (_ : _ @^-1` _ = `]-oo, 1]%classic). + apply/funext => x/=; case: sumbool_ler => /= x0. + case: sumbool_ler => /= x1. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. + by rewrite mem_set//= memNset//= ?simpe x0 x1. + rewrite /bernoulli0. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. + by rewrite mem_set//= memNset//= ?simpe x0/= leNgt x1. + rewrite /bernoulli0. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. + by rewrite mem_set//= memNset//= ?simpe leNgt x0. +- rewrite [X in measurable_fun _ X](_ : _ = (fun x => if 0 <= x <= 1 then (`1-x)%:E else 1%E))//. + apply: measurable_fun_ifT => //=. + apply: measurable_and => //; apply: (measurable_fun_bool true) => //=. + rewrite (_ : _ @^-1` _ = `[0, +oo[%classic)//. + by apply/seteqP; split => [x|x] /=; rewrite in_itv/= andbT. + by rewrite (_ : _ @^-1` _ = `]-oo, 1]%classic). + by apply/EFin_measurable_fun; apply/measurable_funB. + apply/funext => x/=; case: sumbool_ler => /= x0. + case: sumbool_ler => /= x1. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. + by rewrite memNset//= mem_set//= ?simpe x0 x1/=. + rewrite /bernoulli0. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. + by rewrite memNset//= mem_set//= ?simpe x0/= leNgt x1/= onem0. + rewrite /bernoulli0. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. + by rewrite memNset//= mem_set//= leNgt x0/= ?simpe onem0. +- rewrite [X in measurable_fun _ X](_ : _ = (fun=> 1%E))//; apply/funext => x/=. + case: sumbool_ler => /= x0. + case: sumbool_ler => /= x1. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. + rewrite mem_set//=; last by left. + rewrite mem_set//=; last by right. + by rewrite ?simpe -EFinD add_onemK. + rewrite /bernoulli0. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. + rewrite mem_set//=; last by left. + rewrite mem_set//=; last by right. + by rewrite ?simpe onem0. + rewrite /bernoulli0. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. + rewrite mem_set//=; last by left. + rewrite mem_set//=; last by right. + by rewrite ?simpe onem0. +Qed. End bernoulli_trunc. From 8cbd7b2bf252d778e3d31031c0efdb5d1c967465 Mon Sep 17 00:00:00 2001 From: AyumuSaito Date: Fri, 12 Jan 2024 17:43:09 +0900 Subject: [PATCH 13/36] add Nat and 0<=p<=1 problem - also fix casino1 --- theories/lang_syntax.v | 48 +++-- theories/lang_syntax_examples.v | 29 +-- theories/lang_syntax_examples_wip.v | 302 ++++++++++++++++++++++------ theories/prob_lang.v | 64 +++--- 4 files changed, 318 insertions(+), 125 deletions(-) diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index 4a87005e6..1e12682e2 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -266,7 +266,7 @@ Import Notations. Context {R : realType}. Inductive typ := -| Unit | Bool | Real +| Unit | Bool | Nat | Real | Pair : typ -> typ -> typ | Prob : typ -> typ. @@ -276,6 +276,7 @@ Fixpoint measurable_of_typ (t : typ) : {d & measurableType d} := match t with | Unit => existT _ _ munit | Bool => existT _ _ mbool + | Nat => existT _ _ (nat : measurableType _) | Real => existT _ _ [the measurableType _ of (@measurableTypeR R)] (* (Real_sort__canonical__measure_Measurable R) *) @@ -407,22 +408,22 @@ Section relop. Inductive relop := | relop_le | relop_lt | relop_eq . -Definition fun_of_relop g (r : relop) : (@mctx R g -> @mtyp R Real) -> - (mctx g -> mtyp Real) -> @mctx R g -> @mtyp R Bool := +Definition fun_of_relop g (r : relop) : (@mctx R g -> @mtyp R Nat) -> + (mctx g -> mtyp Nat) -> @mctx R g -> @mtyp R Bool := match r with -| relop_le => (fun f1 f2 x => (f1 x <= f2 x)%R) -| relop_lt => (fun f1 f2 x => (f1 x < f2 x)%R) -| relop_eq => (fun f1 f2 x => (f1 x == f2 x)%R) +| relop_le => (fun f1 f2 x => (f1 x <= f2 x)%N) +| relop_lt => (fun f1 f2 x => (f1 x < f2 x)%N) +| relop_eq => (fun f1 f2 x => (f1 x == f2 x)%N) end. Definition mfun_of_relop g r - (f1 : @mctx R g -> @mtyp R Real) (mf1 : measurable_fun setT f1) - (f2 : @mctx R g -> @mtyp R Real) (mf2 : measurable_fun setT f2) : + (f1 : @mctx R g -> @mtyp R Nat) (mf1 : measurable_fun setT f1) + (f2 : @mctx R g -> @mtyp R Nat) (mf2 : measurable_fun setT f2) : measurable_fun [set: @mctx R g] (fun_of_relop r f1 f2). destruct r. -exact: measurable_fun_ler. -exact: measurable_fun_ltr. -exact: measurable_fun_eqr. +exact: measurable_fun_leq. +exact: measurable_fun_ltn. +exact: measurable_fun_eqn. Defined. End relop. @@ -430,12 +431,13 @@ End relop. Inductive exp : flag -> ctx -> typ -> Type := | exp_unit g : exp D g Unit | exp_bool g : bool -> exp D g Bool +| exp_nat g : nat -> exp D g Nat | exp_real g : R -> exp D g Real | exp_pow g : nat -> exp D g Real -> exp D g Real | exp_bin (b : binop) g : exp D g (type_of_binop b) -> exp D g (type_of_binop b) -> exp D g (type_of_binop b) -| exp_rel (r : relop) g : exp D g Real -> - exp D g Real -> exp D g Bool +| exp_rel (r : relop) g : exp D g Nat -> + exp D g Nat -> exp D g Bool | exp_pair g t1 t2 : exp D g t1 -> exp D g t2 -> exp D g (Pair t1 t2) | exp_proj1 g t1 t2 : exp D g (Pair t1 t2) -> exp D g t1 | exp_proj2 g t1 t2 : exp D g (Pair t1 t2) -> exp D g t2 @@ -445,9 +447,9 @@ Inductive exp : flag -> ctx -> typ -> Type := | exp_bernoulli_trunc g : exp D g Real -> exp D g (Prob Bool) | exp_binomial g (n : nat) (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : - exp D g (Prob Real) + exp D g (Prob Nat) | exp_binomial_trunc g (n : nat) : - exp D g Real -> exp D g (Prob Real) + exp D g Real -> exp D g (Prob Nat) | exp_uniform g (a b : R) (ab0 : (0 < b - a)%R) : exp D g (Prob Real) | exp_poisson g : nat -> exp D g Real -> exp D g Real | exp_normalize g t : exp P g t -> exp D g (Prob t) @@ -473,6 +475,7 @@ End syntax_of_expressions. Arguments exp {R}. Arguments exp_unit {R g}. Arguments exp_bool {R g}. +Arguments exp_nat {R g}. Arguments exp_real {R g}. Arguments exp_pow {R g}. Arguments exp_bin {R} b {g} &. @@ -499,6 +502,8 @@ Notation "[ e ]" := e (e custom expr at level 5) : lang_scope. Notation "'TT'" := (exp_unit) (in custom expr at level 1) : lang_scope. Notation "b ':B'" := (@exp_bool _ _ b%bool) (in custom expr at level 1) : lang_scope. +Notation "n ':N'" := (@exp_nat _ _ n%N) + (in custom expr at level 1) : lang_scope. Notation "r ':R'" := (@exp_real _ _ r%R) (in custom expr at level 1, format "r :R") : lang_scope. Notation "e ^+ n" := (exp_pow n e) @@ -557,6 +562,7 @@ Fixpoint free_vars k g t (e : @exp R k g t) : seq string := match e with | exp_unit _ => [::] | exp_bool _ _ => [::] + | exp_nat _ _ => [::] | exp_real _ _ => [::] | exp_pow _ _ e => free_vars e | exp_bin _ _ e1 e2 => free_vars e1 ++ free_vars e2 @@ -694,6 +700,8 @@ Inductive evalD : forall g t, exp D g t -> | eval_bool g b : ([b:B] : exp D g _) -D> cst b ; kb b +| eval_nat g n : ([n:N] : exp D g _) -D> cst n; kn n + | eval_real g r : ([r:R] : exp D g _) -D> cst r ; kr r | eval_pow g n (e : exp D g _) f mf : e -D> f ; mf -> @@ -824,6 +832,9 @@ all: (rewrite {g t e u v mu mv hu}). - move=> g b {}v {}mv. inversion 1; subst g0 b0. by inj_ex H3. +- move=> g n {}v {}mv. + inversion 1; subst g0 n0. + by inj_ex H3. - move=> g r {}v {}mv. inversion 1; subst g0 r0. by inj_ex H3. @@ -974,6 +985,9 @@ all: rewrite {g t e u v eu}. - move=> g b {}v {}mv. inversion 1; subst g0 b0. by inj_ex H3. +- move=> g n {}v {}mv. + inversion 1; subst g0 n0. + by inj_ex H3. - move=> g r {}v {}mv. inversion 1; subst g0 r0. by inj_ex H3. @@ -1120,6 +1134,7 @@ elim: e. all: rewrite {z g t}. - by do 2 eexists; exact: eval_unit. - by do 2 eexists; exact: eval_bool. +- by do 2 eexists; exact: eval_nat. - by do 2 eexists; exact: eval_real. - move=> g n e [f [mf H]]. by exists (fun x => (f x ^+ n)%R); eexists; exact: eval_pow. @@ -1239,6 +1254,9 @@ Proof. exact/execD_evalD/eval_unit. Qed. Lemma execD_bool g b : @execD g _ [b:B] = existT _ (cst b) (kb b). Proof. exact/execD_evalD/eval_bool. Qed. +Lemma execD_nat g n : @execD g _ [n:N] = existT _ (cst n) (kn n). +Proof. exact/execD_evalD/eval_nat. Qed. + Lemma execD_real g r : @execD g _ [r:R] = existT _ (cst r) (kr r). Proof. exact/execD_evalD/eval_real. Qed. diff --git a/theories/lang_syntax_examples.v b/theories/lang_syntax_examples.v index 76b90c12b..c52cc2748 100644 --- a/theories/lang_syntax_examples.v +++ b/theories/lang_syntax_examples.v @@ -457,37 +457,22 @@ Definition sample_binomial3 : @exp R _ [::] _ := return #{"x"}]. Lemma exec_sample_binomial3 t U : measurable U -> - execP sample_binomial3 t U = ((1 / 8)%:E * \d_(0%:R : R) U + - (3 / 8)%:E * \d_(1%:R : R) U + - (3 / 8)%:E * \d_(2%:R : R) U + - (1 / 8)%:E * \d_(3%:R : R) U)%E. + execP sample_binomial3 t U = ((1 / 8)%:E * \d_0%N U + + (3 / 8)%:E * \d_1%N U + + (3 / 8)%:E * \d_2%N U + + (1 / 8)%:E * \d_3%N U)%E. Proof. move=> mU; rewrite /sample_binomial3 execP_letin execP_sample execP_return. rewrite exp_var'E (execD_var_erefl "x") !execD_binomial/=. -rewrite letin'E ge0_integral_measure_sum//=; last first. - exact: measurable_fun_dirac. +rewrite letin'E ge0_integral_measure_sum//=. rewrite !big_ord_recl big_ord0 !ge0_integral_mscale//=; [|exact: measurable_fun_dirac..]. rewrite !integral_dirac// /bump; [|exact: measurable_fun_dirac..]. rewrite !binS/= !bin0 bin1 bin2 bin_small// addn0. rewrite expr0 mulr1 mul1r subn0. rewrite -2!addeA !mul1r. congr _%:E. -rewrite indicT !mul1r. -congr (_ + _). - congr (_ * _). - by field. -congr (_ + _). - congr (_ * _). - rewrite expr1 /onem. - by field. -congr (_ + _). - congr (_ * _). - rewrite /onem/=. - by field. -rewrite addr0. -congr (_ * _). -rewrite /onem/=. -by field. +rewrite indicT !mul1r /onem !addrA addr0 expr1/=. +by congr (_ + _ + _ + _); congr (_ * _); field. Qed. End binomial_examples. diff --git a/theories/lang_syntax_examples_wip.v b/theories/lang_syntax_examples_wip.v index c2fc64319..9e0c7b119 100644 --- a/theories/lang_syntax_examples_wip.v +++ b/theories/lang_syntax_examples_wip.v @@ -25,12 +25,15 @@ Open Scope lang_scope. Context (R : realType). Lemma a01 : 0 < 1 - 0 :> R. Proof. by []. Qed. +(* Definition ex : exp _ [::] _ := @exp_bernoulli R [::] (1 / 2)%:nng (p1S 1). +Example ex1 : projT1 (execD ex) tt = 1%:E. *) + Definition casino0 : exp _ [::] Bool := [let "p" := Sample {exp_uniform 0 1 a01} in let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in - let "_" := if #{"a1"} == {5}:R then return TT else Score {0}:R in + let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in let "a2" := Sample {exp_binomial_trunc 3 [#{"p"}]} in - return {1}:R <= #{"a2"}]. + return {1}:N <= #{"a2"}]. Example e1 : @exp R _ [::] _ := [{1}:R + {2}:R * {2}:R ^+ {3%nat}]. @@ -46,20 +49,136 @@ Qed. Definition casino1 : @exp R _ [::] _ := [let "p" := Sample {exp_uniform 0 1 a01} in let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in - let "_" := if #{"a1"} == {5}:R then return TT else Score {0}:R in - let "a2" := Sample - {exp_bernoulli_trunc [{1}:R - #{"p"} ^+ {3%nat}]} in + let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in + let "a2" := + Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]} in return #{"a2"}]. +Definition casino1' : @exp R _ [::] _ := + [let "p" := Sample {exp_uniform 0 1 a01} in + let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in + let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in + Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. + +Lemma bernoulli_truncE (p : R) U : + (0 <= p <= 1)%R -> + (bernoulli_trunc p U = + p%:E * \d_true U + (`1-p)%:E * \d_false U)%E. +Proof. +move=> /andP[p0 p1]. +rewrite /bernoulli_trunc. +case: (sumbool_ler 0 p) => [{}p0/=|]. + case: (sumbool_ler p 1) => [{}p1/=|]. + by rewrite /bernoulli/= measure_addE. + by rewrite ltNge p1. +by rewrite ltNge p0. +Qed. + +Lemma binomial_le1' n p U : + 0 <= p <= 1 -> + (\int[binomial_probability_trunc n p]_y0 \d_(0 < y0)%N U = + bernoulli_trunc (1 - `1-p ^+ n) U :> \bar R)%E. +Proof. +move=> /andP[p0 p1]. +rewrite bernoulli_truncE; last first. + apply/andP; split. + apply/onemX_ge0; rewrite /onem; lra. + apply/onem_le1/exprn_ge0; rewrite /onem; lra. +rewrite (@integral_binomial_probabilty_trunc _ n p _ _ (fun y => \d_(1 <= y)%N U))//; last first. +rewrite !big_ord_recl/=. +rewrite /bump. +under eq_bigr => i _. + rewrite /=. + have -> : (0 < 1 + i)%N => //. + over. +rewrite addeC -ge0_sume_distrl. + congr (_ + _)%E; congr (_ * _)%E. + have -> : (\sum_(i < n) (p ^+ (1 + i) * `1-p ^+ (n - (1 + i)) *+ 'C(n, 1 + i))%:E)%E = + (\sum_(i < n.+1) (p ^+ i * `1-p ^+ (n - i) *+ 'C(n, i))%:E - (`1-p ^+ n)%:E)%E. + rewrite big_ord_recl/= expr0 subn0 mul1r bin0 mulr1n addeC addeA. + have <- : 0%E = ((- `1-p ^+ n)%:E + (`1-p ^+ n)%:E)%E. + rewrite EFinN. + congr _%:E. + lra. + by rewrite add0e. + congr _%E. + rewrite sumEFin. + rewrite !EFinB EFin_expe. + congr (_ - _)%E. + under eq_bigr do rewrite mulrC. + rewrite -(@exprDn_comm _ `1-p p n); last first. + by rewrite /GRing.comm/onem; lra. + rewrite /onem addrC. + have -> : p + (1 - p) = 1 by lra. + by rewrite expr1n. + rewrite subn0 expr0 bin0 mulr1n. + rewrite /onem. + congr _%:E. + set pn := (1-p) ^+ n. + lra. +move=> i _. +apply/mulrn_wge0/mulr_ge0; apply/exprn_ge0. +exact: p0. +apply/onem_ge0/p1. +Qed. + +Lemma binomial_le1 n p U : + 0 <= p <= 1 -> + (\int[binomial_probability_trunc n p]_y0 \d_(0 < y0)%N U = + \int[bernoulli_trunc (1 - `1-p ^+ n)]_y0 \d_y0 U :> \bar R)%E. +Proof. +move=> /andP[p0 p1]. +rewrite (@integral_bernoulli_trunc _ _ (fun x => \d_x U))//; last first. + apply/andP; split. + apply: onemX_ge0; rewrite /onem; lra. + apply/onem_le1/exprn_ge0; rewrite /onem; lra. +rewrite (@integral_binomial_probabilty_trunc _ n p _ _ (fun y => \d_(1 <= y)%N U))//; last first. +rewrite !big_ord_recl/=. +rewrite /bump. +under eq_bigr => i _. + rewrite /=. + have -> : (0 < 1 + i)%N => //. + over. +rewrite addeC -ge0_sume_distrl. + congr (_ + _)%E; congr (_ * _)%E. + have -> : (\sum_(i < n) (p ^+ (1 + i) * `1-p ^+ (n - (1 + i)) *+ 'C(n, 1 + i))%:E)%E = + (\sum_(i < n.+1) (p ^+ i * `1-p ^+ (n - i) *+ 'C(n, i))%:E - (`1-p ^+ n)%:E)%E. + rewrite big_ord_recl/= expr0 subn0 mul1r bin0 mulr1n addeC addeA. + have <- : 0%E = ((- `1-p ^+ n)%:E + (`1-p ^+ n)%:E)%E. + rewrite EFinN. + congr _%:E. + lra. + by rewrite add0e. + congr _%E. + rewrite sumEFin. + rewrite !EFinB EFin_expe. + congr (_ - _)%E. + under eq_bigr do rewrite mulrC. + rewrite -(@exprDn_comm _ `1-p p n); last first. + by rewrite /GRing.comm/onem; lra. + rewrite /onem addrC. + have -> : p + (1 - p) = 1 by lra. + by rewrite expr1n. + rewrite subn0 expr0 bin0 mulr1n. + rewrite /onem. + congr _%:E. + set pn := (1-p) ^+ n. + lra. +move=> i _. +apply/mulrn_wge0/mulr_ge0; apply/exprn_ge0. +exact: p0. +apply/onem_ge0/p1. +Qed. + (* exec [let x := sample (binomial n e) in *) (* return (x >= 1)] = *) (* exec [let y := sample (bernoulli (1 - e^n)) in *) (* return y] *) -Lemma binomial_le1 x y n g e t U : +(* Lemma binomial_le1' x y n g e t U : (0 <= (projT1 (execD e) t : mtyp Real) <= 1) -> execP ([let x := Sample {exp_binomial_trunc n e} in - return {1}:R <= #x] : @exp R _ g _) t U = - execP [let y := Sample {exp_bernoulli_trunc [{1}:R - e ^+ n]} in + return {1}:N <= #x] : @exp R _ g _) t U = + execP [let y := Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - e]} ^+ n]} in return #y] t U. Proof. rewrite !execP_letin !execP_sample execD_binomial_trunc execD_bernoulli_trunc/=. @@ -70,20 +189,19 @@ move=> H0 H1. rewrite !execP_return !execD_rel. have /= := @execD_var R ((y, Bool) :: g) y. rewrite eqxx => /(_ H0) ->. - have /= := @execD_var R ((x, Real) :: g) x. + have /= := @execD_var R ((x, Nat) :: g) x. rewrite eqxx => /(_ H1) -> /=. -rewrite (@execD_bin _ _ binop_minus) execD_pow !execD_real/=. +rewrite (@execD_bin _ _ binop_minus) execD_pow (@execD_bin _ _ binop_minus). +rewrite !execD_real execD_nat/=. rewrite 2!letin'E/=. set p := projT1 (execD e) t. move => /andP[p0 p1]. rewrite (@integral_bernoulli_trunc _ _ (fun x => \d_x U))//; last first. apply/andP; split. - exact: (onemX_ge0 _ p0 p1). - apply/onem_le1/exprn_ge0/p0. -rewrite (@integral_binomial_probabilty_trunc _ n p _ _ (fun y => \d_(1 <= y)%R U))//; last first. - (* move=>/= _ y0 my0. - rewrite setTI. *) - apply: measurable_fun_dirac. + apply/onemX_ge0; lra. + apply/onem_le1/exprn_ge0; lra. +rewrite (@integral_binomial_probabilty_trunc _ n p _ _ (fun y => \d_(1 <= y)%N U))//; last first. + (* apply: measurable_fun_dirac. have := @subsetT _ U; rewrite setT_bool => UT. have [->|->|->|->] /= := subset_set2 UT. exact: measurable0. @@ -94,9 +212,15 @@ rewrite (@integral_binomial_probabilty_trunc _ n p _ _ (fun y => \d_(1 <= y)%R U apply/seteqP. admit. admit. - (* rewrite diracE. *) + rewrite diracE. *) rewrite !big_ord_recl/=. -have -> : (1 <= 0 :> R) = false by lra. +rewrite addeC /bump/=. +congr (_ + _)%E. + under eq_bigr => i _. + have -> : (0 < 1 + i)%N => //. + over. +congr (_ * _)%E. +have -> : (0 < 0)%N = false. rewrite /bump. under eq_bigr => i _. rewrite /= natrD. @@ -132,7 +256,7 @@ move=> i _. apply/mulrn_wge0/mulr_ge0; apply/exprn_ge0. exact: p0. apply/onem_ge0/p1. -Admitted. +Admitted. *) Lemma __ : uniform_probability a01 `[0, (1 / 2)] = (1 / 2)%:E. Proof. @@ -154,11 +278,17 @@ transitivity ( by []. Admitted. -Lemma execP_letin_uniform g u a b p (Hap : infer (a != p)) -(Hbp : infer (b != p)) (s0 s1 : @exp R D ((a, Unit) :: (b, Real) :: (p, Real) :: g) Real -> exp P ((p, Real) :: g) u) : - (forall (t : R) x U, t \in `[0, 1] -> execP (s0 [#p]) (t, x) U = execP (s1 [#p]) (t, x) U) -> - execP [let p := Sample {@exp_uniform _ g 0 1 a01} in {s0 [#p]}] = - execP [let p := Sample {@exp_uniform _ g 0 1 a01} in {s1 [#p]}]. +Let weak_head fl g {t1 t2} x (e : @exp R fl g t2) (xg : x \notin dom g) := + exp_weak fl [::] _ (x, t1) e xg. + +Lemma execP_letin_uniform g u p + (* (Hap : infer (a != p)) (Hbp : infer (b != p)) *) + (* (bg : b \notin dom ((p, Real) :: g)) (ag : b \notin dom ((b, Nat) :: (p, Real) :: g)) *) + (s0 s1 : exp P ((p, Real) :: g) u) : + (forall (t : R) x U, 0 <= t <= 1 -> + execP s0 (t, x) U = execP s1 (t, x) U) -> + execP [let p := Sample {@exp_uniform _ g 0 1 a01} in {s0}] = + execP [let p := Sample {@exp_uniform _ g 0 1 a01} in {s1}]. Proof. move=> s01. rewrite !execP_letin execP_sample execD_uniform/=. @@ -170,47 +300,93 @@ apply: s01. by rewrite inE in t01. Qed. -Lemma casino01 : execP casino0 = execP casino1. +(* Lemma casino01 : execP casino0 = execP casino1. Proof. rewrite /casino0 /casino1. -pose s0 := fun x => [let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} - in let "_" := if #{"a1"} == {5}:R then return TT else Score {0}:R - in let "a2" := Sample {exp_binomial_trunc 3 [x]} in {exp_return [{1}:R <= #{"a2"}]}] : @exp R _ _ _. -pose s1 := fun x => [let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} - in let "_" := if #{"a1"} == {5}:R then return TT else Score {0}:R - in let "a2" - := Sample {exp_bernoulli_trunc [{1}:R - x ^+ {3%N}]} - in {exp_return [#{"a2"}]}] : @exp R _ _ _. -apply: (@execP_letin_uniform [::] _ _ _ _ _ _ (s0 _) (s1 _)). -move=> e k k01 ek. -rewrite /s0/s1. -rewrite 2!execP_letin. -rewrite 2![in RHS]execP_letin. -congr (letin' _ _ _ _). -congr (letin' _ _ _). -(* apply: eq_sfkernel => x U. *) -rewrite !execP_letin !execP_sample execD_bernoulli_trunc execD_binomial_trunc/=. -rewrite (@execD_bin _ _ binop_minus) !execP_return execD_rel execD_pow/=. -rewrite !execD_real !exp_var'E !(execD_var_erefl "a2") (execD_var_erefl "p")/=. - -have H := (@binomial_le1 "a2" "a2" 3 _ [#{"p"}]). -(* f_equal. *) -(* congr ((letin' _ _ _) _). -apply: eq_sfkernel => ?. -have : 0 <= e <= 1. - admit. -move=> H. -rewrite (binomial_le1 _ _ _ _ H). -under eq_integral. -rewrite execP_letin. -congr (letin'). - -(* apply: eq_sfkernel => x U. *) -have : 0 <= (projT1 (execD e) x : mtyp Real) <= 1. - by rewrite ek. -move=> H. -apply: (binomial_le1 _ _ _ _ H). *) -Admitted. +rewrite !execP_letin !execP_sample execD_uniform !execD_binomial_trunc /=. +rewrite execP_if execP_score !execP_return !execD_bernoulli_trunc/=. +rewrite !execD_rel (@execD_bin _ _ binop_minus) !execD_real execD_pow/=. +rewrite !execD_nat execD_unit/=. +rewrite !exp_var'E !(execD_var_erefl "p") !(execD_var_erefl "a1")/=. +rewrite !(execD_var_erefl "a2")/=. +do 3 congr letin'. +apply: eq_sfkernel => x U. +rewrite !letin'E/=. +apply: binomial_le1. + +rewrite /=. +Abort. *) + +(* Lemma casino01 : execP casino0 = execP casino1. +Proof. +rewrite /casino0 /casino1. +pose s0 := + [let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in + let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in + let "a2" := Sample {exp_binomial_trunc 3 [#{"p"}]} in + {exp_return [{1}:N <= #{"a2"}]}]. +pose s1 := + [let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in + let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in + let "a2" := Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%N}]} in + {exp_return [#{"a2"}]}]. +have := (@execP_letin_uniform [::] Bool "p" (s0 R (found "p" Real [::]) _ _) (s1 R (found "p" Real [::]) _ _)). +apply. +move=> p x U r01. +rewrite /s0/s1/=. +rewrite !execP_letin !execP_sample !execD_binomial_trunc /=. +rewrite execP_if execP_score !execP_return !execD_bernoulli_trunc/=. +rewrite !execD_rel (@execD_bin _ _ binop_minus) execD_pow. +rewrite (@execD_bin _ _ binop_minus) !execD_real/=. +rewrite !execD_nat execD_unit/=. +rewrite !exp_var'E !(execD_var_erefl "p") !(execD_var_erefl "a1")/=. +rewrite !(execD_var_erefl "a2")/=. +rewrite !letin'E/=. +move: r01 => /andP[r0 r1]. +rewrite !integral_binomial_probabilty_trunc//=. +apply: eq_bigr => i _. +congr (_ * _)%E. +rewrite !letin'E iteE/=. +congr (\int[_]_y _)%E. +apply: funext => x0. +rewrite !letin'E/=. +by apply/binomial_le1/andP. +Qed. *) + +Lemma casino01' : execP casino0 = execP casino1'. +Proof. +rewrite /casino0 /casino1. +pose s0 := + [let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in + let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in + let "a2" := Sample {exp_binomial_trunc 3 [#{"p"}]} in + return {1}:N <= #{"a2"}]. +(* pose s1 := + [let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in + let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in + return {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%N}]}]. *) +have := (@execP_letin_uniform [::] Bool "p" (s0 R (found "p" Real [::]) _ _) _). +apply. +move=> p x U r01. +rewrite /s0/=. +rewrite !execP_letin !execP_sample !execD_binomial_trunc /=. +rewrite execP_if execP_score !execP_return !execD_bernoulli_trunc/=. +rewrite !execD_rel (@execD_bin _ _ binop_minus) execD_pow. +rewrite (@execD_bin _ _ binop_minus) !execD_real/=. +rewrite !execD_nat execD_unit/=. +rewrite !exp_var'E !(execD_var_erefl "p") !(execD_var_erefl "a1")/=. +rewrite !(execD_var_erefl "a2")/=. +rewrite !letin'E/=. +move: r01 => /andP[r0 r1]. +rewrite !integral_binomial_probabilty_trunc//=. +apply: eq_bigr => i _. +congr (_ * _)%E. +rewrite !letin'E iteE/=. +congr (\int[_]_y _)%E. +apply: funext => x0. +rewrite !letin'E/=. +by apply/binomial_le1'/andP. +Qed. (* guard test *) Definition test_guard : @exp R _ [::] _ := [ diff --git a/theories/prob_lang.v b/theories/prob_lang.v index a75d4fa4b..017e959d3 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -61,6 +61,7 @@ Notation var3of3 := (@measurable_snd _ _ _ _).*) salgebraType (R.-ocitv.-measurable)]. *) Notation munit := Datatypes_unit__canonical__measure_Measurable. Notation mbool := Datatypes_bool__canonical__measure_Measurable. +Notation mnat := Datatypes_nat__canonical__measure_Measurable. End Notations. (* TODO: PR *) @@ -137,7 +138,7 @@ Arguments p1S {R}. Section bernoulli. Variables (R : realType) (p : {nonneg R}) (p1 : (p%:num <= 1)%R). -Definition bernoulli : set _ -> \bar R := +Definition bernoulli : set bool -> \bar R := measure_add [the measure _ _ of mscale p [the measure _ _ of dirac true]] [the measure _ _ of mscale (onem_nonneg p1) [the measure _ _ of dirac false]]. @@ -212,10 +213,7 @@ have [->|->|->|->] /= := subset_set2 UT. apply/funext => x/=. by case: sumbool_ler. - rewrite [X in measurable_fun _ X](_ : _ = (fun x => if 0 <= x <= 1 then x%:E else 0%E))//. - apply: measurable_fun_ifT => //=; apply: measurable_and => //; apply: (measurable_fun_bool true) => //=. - rewrite (_ : _ @^-1` _ = `[0, +oo[%classic)//. - by apply/seteqP; split => [x|x] /=; rewrite in_itv/= andbT. - by rewrite (_ : _ @^-1` _ = `]-oo, 1]%classic). + by apply: measurable_fun_ifT => //=; apply: measurable_and => //; exact: measurable_fun_ler. apply/funext => x/=; case: sumbool_ler => /= x0. case: sumbool_ler => /= x1. rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. @@ -228,11 +226,8 @@ have [->|->|->|->] /= := subset_set2 UT. by rewrite mem_set//= memNset//= ?simpe leNgt x0. - rewrite [X in measurable_fun _ X](_ : _ = (fun x => if 0 <= x <= 1 then (`1-x)%:E else 1%E))//. apply: measurable_fun_ifT => //=. - apply: measurable_and => //; apply: (measurable_fun_bool true) => //=. - rewrite (_ : _ @^-1` _ = `[0, +oo[%classic)//. - by apply/seteqP; split => [x|x] /=; rewrite in_itv/= andbT. - by rewrite (_ : _ @^-1` _ = `]-oo, 1]%classic). - by apply/EFin_measurable_fun; apply/measurable_funB. + by apply: measurable_and => //; exact: measurable_fun_ler. + by apply/EFin_measurable_fun; apply/measurable_funB. apply/funext => x/=; case: sumbool_ler => /= x0. case: sumbool_ler => /= x1. rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. @@ -287,11 +282,11 @@ Context {R : realType} (n : nat) (p : {nonneg R}) (p1 : (p%:num <= 1)%R). Local Open Scope ring_scope. (* C(n, k) * p^(n-k) * (1-p)^k *) -Definition bino_term (k : nat) :{nonneg R} := - (p%:num^+(n-k)%N * (NngNum (onem_ge0 p1))%:num^+k *+ 'C(n, k))%:nng. +Definition bino_term (k : nat) : {nonneg R} := + (p%:num^+k * (NngNum (onem_ge0 p1))%:num^+(n-k)%N *+ 'C(n, k))%:nng. Lemma bino_term0 : - bino_term 0 = (p%:num^+n)%:nng. + bino_term 0 = ((NngNum (onem_ge0 p1))%:num^+n)%:nng. Proof. rewrite /bino_term bin0 subn0/=. apply/val_inj => /=. @@ -299,18 +294,22 @@ by field. Qed. Lemma bino_term1 : - bino_term 1 = (p%:num^+(n-1)%N * (NngNum (onem_ge0 p1))%:num *+ n)%:nng. + bino_term 1 = (p%:num * (NngNum (onem_ge0 p1))%:num^+(n-1)%N *+ n)%:nng. Proof. rewrite /bino_term bin1/=. apply/val_inj => /=. by rewrite expr1. Qed. +Import Notations. + +(* Check \sum_(k < n.+1) (fun k => [the measure _ _ of mscale (bino_term k) + [the measure _ _ of \d_k]]). *) (* \sum_(k < n.+1) (bino_coef p n k) * \d_k. *) -Definition binomial_probability : set (measurableTypeR R) -> \bar R := +Definition binomial_probability : set nat -> \bar R := @msum _ _ R (fun k => [the measure _ _ of mscale (bino_term k) - [the measure _ _ of @dirac _ (measurableTypeR R) k%:R R]]) n.+1. + [the measure _ _ of \d_k]]) n.+1. HB.instance Definition _ := Measure.on binomial_probability. @@ -319,8 +318,11 @@ Proof. rewrite /binomial_probability/msum/mscale/bino_term/=/mscale/=. under eq_bigr do rewrite diracT mule1. rewrite sumEFin. +under eq_bigr=> i _. + rewrite mulrC. + over. rewrite -exprDn_comm; last by rewrite /GRing.comm mulrC. -by rewrite add_onemK; congr _%:E; rewrite expr1n. +by rewrite addrC add_onemK; congr _%:E; rewrite expr1n. Qed. HB.instance Definition _ := @@ -332,10 +334,10 @@ Section integral_binomial. Variables (R : realType) (d : measure_display) (T : measurableType d). Lemma integral_binomial (n : nat) (p : {nonneg R}) - (p1 : (p%:num <= 1)%R) (f : R -> \bar R) + (p1 : (p%:num <= 1)%R) (f : nat -> \bar R) (mf : measurable_fun setT f) : (forall x, 0 <= f x) -> \int[binomial_probability n p1]_y (f y) = - \sum_(k < n.+1) (bino_term n p1 k)%:num%:E * f k%:R. + \sum_(k < n.+1) (bino_term n p1 k)%:num%:E * f k. Proof. move=> f0. rewrite ge0_integral_measure_sum//=; last first. @@ -365,6 +367,14 @@ Definition binomial_probability_trunc n (p : R) := Lemma measurable_binomial_probability_trunc (n : nat) : measurable_fun setT (binomial_probability_trunc n : R -> pprobability _ _). +Proof. +apply: (@measurability _ _ _ _ _ _ + (@pset _ _ _ : set (set (pprobability _ R)))) => //. +move=> _ -[_ [r r01] [Ys mYs <-]] <-. +apply: emeasurable_fun_infty_o => //=. +rewrite /binomial_probability_trunc/=. +(* have := @subsetT _ Ys; rewrite setT_bool => UT. +have [->|->|->|->] /= := subset_set2 UT. *) Admitted. End binomial_trunc. @@ -375,9 +385,10 @@ Arguments measurable_binomial_probability_trunc {R}. Section integral_binomial_trunc. Variables (R : realType) (d : measure_display) (T : measurableType d). +Import Notations. Lemma integral_binomial_probabilty_trunc (n : nat) (p : R) - (p0 : (0 <= p)%R) (p1 : ((NngNum p0)%:num <= 1)%R) (f : R -> \bar R) (mf : measurable_fun setT f) : - (forall x, 0 <= f x) -> \int[binomial_probability_trunc n p]_y (f y) = \sum_(k < n.+1) (bino_term n p1 k)%:num%:E * f k%:R. + (p0 : (0 <= p)%R) (p1 : ((NngNum p0)%:num <= 1)%R) (f : mnat -> \bar R) (mf : measurable_fun setT f) : + (forall x, 0 <= f x) -> \int[binomial_probability_trunc n p]_y (f y) = \sum_(k < n.+1) (bino_term n p1 k)%:num%:E * f k. Proof. move=> f0. rewrite /binomial_probability_trunc/=. @@ -394,15 +405,15 @@ Section binomial_example. Context {R : realType}. Open Scope ring_scope. -Lemma binomial3_2 : @binomial_probability R 3 _ (p1S 1) [set 2%:R] = (3 / 8)%:E. +Lemma binomial3_2 : @binomial_probability R 3 _ (p1S 1) [set 2%N] = (3 / 8)%:E. Proof. rewrite /binomial_probability/msum !big_ord_recl/= big_ord0 adde0 bino_term0. rewrite /mscale/= !diracE /bump/=. repeat rewrite ?binS ?bin0 ?bin1 ?bin_small//. -rewrite memNset//=; last by move/eqP; rewrite eqr_nat. -rewrite memNset//=; last by move/eqP; rewrite eqr_nat. +rewrite memNset//=. +rewrite memNset//=. rewrite mem_set//=. -rewrite memNset//=; last by move/eqP; rewrite eqr_nat. +rewrite memNset//=. congr _%:E. rewrite expr0 !mulr1 !mulr0 !add0r !addn0 !add0n /onem. by field. @@ -930,12 +941,15 @@ Definition k10 : measurable_fun _ _ := kr 10%:R. Definition ktt := @measurable_cst _ _ T _ setT tt. Definition kb (b : bool) := @measurable_cst _ _ T _ setT b. +Definition kn (n : nat) := @measurable_cst _ _ T _ setT n. + End cst_fun. Arguments kr {d T R}. Arguments k3 {d T R}. Arguments k10 {d T R}. Arguments ktt {d T}. Arguments kb {d T}. +Arguments kn {d T}. Section iter_mprod. Import Notations. From 418b3af07b6794b5b04cfe5028c698141ffa99b5 Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Tue, 16 Jan 2024 09:44:11 +0900 Subject: [PATCH 14/36] progress - several admits proved - patch - admits in examples wip --- theories/lang_syntax_examples_wip.v | 121 +++++----- theories/lebesgue_measure.v | 5 +- theories/prob_lang.v | 348 ++++++++++++++++++---------- 3 files changed, 279 insertions(+), 195 deletions(-) diff --git a/theories/lang_syntax_examples_wip.v b/theories/lang_syntax_examples_wip.v index 9e0c7b119..cae3d8259 100644 --- a/theories/lang_syntax_examples_wip.v +++ b/theories/lang_syntax_examples_wip.v @@ -266,17 +266,20 @@ Abort. Lemma letin'_sample_uniform d d' (T : measurableType d) (T' : measurableType d') (a b : R) (ab0 : (0 < b - a)%R) (u : R.-sfker [the measurableType _ of (_ * T)%type] ~> T') x y : + measurable y -> letin' (sample_cst (uniform_probability ab0)) u x y = - (((b - a)^-1)%:E * \int[lebesgue_measure]_(x0 in `[a, b]) u (x0, x) y)%E. + ((b - a)^-1%:E * \int[lebesgue_measure]_(x0 in `[a, b]) u (x0, x) y)%E. Proof. -rewrite letin'E/=. -rewrite ge0_integral_mscale//=; last admit. -transitivity ( -(((b - a)^-1)%:E * \int[lebesgue_measure]_(x0 in `[a, b]) u (x0, x) y)%E -). - admit. -by []. -Admitted. +move=> my; rewrite letin'E/=. +rewrite integral_uniform//= => _ /= Y mY /=. +have /= := measurable_kernel u _ my measurableT _ mY. +move/measurable_ysection => /(_ R x) /=. +set A := (X in measurable X). +set B := (X in _ -> measurable X). +suff : A = B by move=> ->. +rewrite {}/A {}/B !setTI /ysection/= (*TODO: lemma?*) /preimage/=. +by apply/seteqP; split => [z|z] /=; rewrite inE/=. +Qed. Let weak_head fl g {t1 t2} x (e : @exp R fl g t2) (xg : x \notin dom g) := exp_weak fl [::] _ (x, t1) e xg. @@ -287,13 +290,13 @@ Lemma execP_letin_uniform g u p (s0 s1 : exp P ((p, Real) :: g) u) : (forall (t : R) x U, 0 <= t <= 1 -> execP s0 (t, x) U = execP s1 (t, x) U) -> - execP [let p := Sample {@exp_uniform _ g 0 1 a01} in {s0}] = - execP [let p := Sample {@exp_uniform _ g 0 1 a01} in {s1}]. + forall x U, measurable U -> + execP [let p := Sample {@exp_uniform _ g 0 1 a01} in {s0}] x U = + execP [let p := Sample {@exp_uniform _ g 0 1 a01} in {s1}] x U. Proof. -move=> s01. +move=> s01 x U mU. rewrite !execP_letin execP_sample execD_uniform/=. -apply: eq_sfkernel => x U. -rewrite 2!letin'_sample_uniform. +rewrite !letin'_sample_uniform//. congr (_ * _)%E. apply: eq_integral => t t01. apply: s01. @@ -353,8 +356,9 @@ rewrite !letin'E/=. by apply/binomial_le1/andP. Qed. *) -Lemma casino01' : execP casino0 = execP casino1'. +Lemma casino01' y V : measurable V -> execP casino0 y V = execP casino1' y V. Proof. +move=> mV. rewrite /casino0 /casino1. pose s0 := [let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in @@ -366,7 +370,7 @@ pose s0 := let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in return {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%N}]}]. *) have := (@execP_letin_uniform [::] Bool "p" (s0 R (found "p" Real [::]) _ _) _). -apply. +apply => //. move=> p x U r01. rewrite /s0/=. rewrite !execP_letin !execP_sample !execD_binomial_trunc /=. @@ -395,7 +399,7 @@ Definition test_guard : @exp R _ [::] _ := [ return #{"p"} ]. -Lemma exec_guard t U : execP test_guard t U = ((1 / 3)%:E * @dirac _ _ true R U)%E. +Lemma exec_guard t U : execP test_guard t U = ((1 / 3)%:E * \d_true U)%E. Proof. rewrite /test_guard 2!execP_letin execP_sample execD_bernoulli execP_if/=. rewrite !execP_return !exp_var'E !(execD_var_erefl "p") execD_unit execP_score execD_real/=. @@ -406,8 +410,7 @@ by rewrite normr0 mul0e !mule0 !adde0 !diracT !mul1e. Qed. Lemma exec_casino t U : - execP casino0 t U = ((10 / 99)%:E * @dirac _ _ true R U + - (1 / 99)%:E * @dirac _ _ false R U)%E . + execP casino0 t U = ((10 / 99)%:E * \d_true U + (1 / 99)%:E * \d_false U)%E . Proof. rewrite /casino0 !execP_letin !execP_sample execD_uniform/=. rewrite !execD_binomial_trunc execP_if !execP_return !execP_score/=. @@ -417,15 +420,12 @@ rewrite (execD_var_erefl "p") (execD_var_erefl "a2")/=. rewrite letin'E/= /uniform_probability ge0_integral_mscale//=. rewrite subr0 invr1 mul1e. under eq_integral. - Admitted. Definition uniform_syntax : @exp R _ [::] _ := [let "p" := Sample {exp_uniform 0 1 a01} in return #{"p"}]. -About integralT_nnsfun. - Lemma exec_uniform_syntax t U : measurable U -> execP uniform_syntax t U = uniform_probability a01 U. Proof. @@ -433,33 +433,22 @@ move=> mU. rewrite /uniform_syntax execP_letin execP_sample execP_return !execD_uniform. rewrite exp_var'E (execD_var_erefl "p")/=. rewrite letin'E /=. +rewrite integral_uniform//=; last exact: measurable_fun_dirac. +rewrite subr0 invr1 mul1e. rewrite {1}/uniform_probability. -set x := (X in mscale _ X). -set k := (X in mscale X _). -transitivity ((k%:num)%:E * \int[x]_y \d_y U)%E. - rewrite -ge0_integral_mscale //. - exact: measurable_fun_dirac. -rewrite /uniform_probability /mscale /=. -congr (_ * _)%E. -under eq_integral do rewrite diracE. -rewrite /=. -rewrite /lebesgue_measure/=. -rewrite -integral_cst. -set f := (mrestr lebesgue_measure (measurable_itv `[0%R, 1%R])). -admit. -rewrite -(@ge0_integral_mscale _ _ _ x setT measurableT k (fun y => \d_y U)) //. -rewrite /uniform_probability/mscale/=/integral//=. -Admitted. +rewrite /mscale/= subr0 invr1 mul1e. +by rewrite integral_indic. +Qed. Definition binomial_le : @exp R _ [::] Bool := [let "a2" := Sample {exp_binomial 3 (1 / 2)%:nng (p1S 1)} in - return {1}:R <= #{"a2"}]. + return {1}:N <= #{"a2"}]. Lemma exec_binomial_le t U : - execP binomial_le t U = ((7 / 8)%:E * @dirac _ _ true R U + - (1 / 8)%:E * @dirac _ _ false R U)%E. + execP binomial_le t U = ((7 / 8)%:E * \d_true U + + (1 / 8)%:E * \d_false U)%E. Proof. -rewrite /binomial_le execP_letin execP_sample execP_return execD_rel execD_real. +rewrite /binomial_le execP_letin execP_sample execP_return execD_rel execD_nat. rewrite exp_var'E (execD_var_erefl "a2") execD_binomial. rewrite letin'E//= /binomial_probability ge0_integral_measure_sum//=. rewrite !big_ord_recl big_ord0 !ge0_integral_mscale//=. @@ -467,39 +456,39 @@ rewrite !integral_dirac// /bump. rewrite !binS/= !bin0 bin1 bin2 bin_small// addn0. rewrite addeC adde0. congr (_ + _)%:E. -have -> : (1 <= 1)%R. admit. -have -> : (1 <= 2)%R. admit. -have -> : (1 <= 3)%R. admit. -rewrite -!mulrDl indicT !mul1r. -congr (_ * _). -rewrite /onem addn0 add0n. -by field. -congr (_ * _). -by field. -(* by rewrite ler10. *) -Admitted. + rewrite !indicT !(mul0n,add0n,lt0n,mul1r)/=. + rewrite -!mulrDl; congr (_ * _). + rewrite /onem. + lra. +rewrite !expr0 ltnn indicT/= !(mul1r,mul1e) /onem. +lra. +Qed. -Definition binomial_guard : @exp R _ [::] Real := +Definition binomial_guard : @exp R _ [::] Nat := [let "a1" := Sample {exp_binomial 3 (1 / 2)%:nng (p1S 1)} in - let "_" := if #{"a1"} == {1}:R then return TT else Score {0}:R in + let "_" := if #{"a1"} == {1}:N then return TT else Score {0}:R in return #{"a1"}]. Lemma exec_binomial_guard t U : - execP binomial_guard t U = ((7 / 8)%:E * @dirac _ R 1%R R U + - (1 / 8)%:E * @dirac _ R 0%R R U)%E. + execP binomial_guard t U = ((3 / 8)%:E * \d_1%N U(* + + (1 / 8)%:E * \d_0%N U*))%E. Proof. rewrite /binomial_guard !execP_letin execP_sample execP_return execP_if. -rewrite !exp_var'E execD_rel !(execD_var_erefl "a1") execP_return execD_unit execD_binomial execD_real execP_score execD_real. +rewrite !exp_var'E execD_rel !(execD_var_erefl "a1") execP_return. +rewrite execD_unit execD_binomial execD_nat execP_score execD_real. rewrite !letin'E//= /binomial_probability ge0_integral_measure_sum//=. rewrite !big_ord_recl big_ord0 !ge0_integral_mscale//=. -rewrite !integral_dirac// /bump. -rewrite (* indicT *) !binS/= !bin0 bin1 bin2 bin_small// addn0 (* !mul1e *). +rewrite !integral_dirac//. +rewrite /bump/=. +rewrite !binS/= !bin0 bin1 bin2 bin_small//. +rewrite !diracT !addn0 !expr0 !subn0 !mulr1n !mul1r !expr1 !mul1e. rewrite !letin'E//= !iteE/= !diracE/=. -have -> : (0 == 1)%R = false; first by admit. -have -> : (1 == 1)%R; first by admit. -have -> : (2 == 1)%R = false; first by admit. -have -> : (3 == 1)%R = false; first by admit. -rewrite addeC adde0. -Admitted. +rewrite !ge0_integral_mscale//=. +rewrite !integral_dirac// !diracT//. +rewrite !(normr0,mul0e,mule0,add0e,add0n,mul1e,adde0). +rewrite /onem. +congr (_%:E * _)%E. +lra. +Qed. End casino_example. diff --git a/theories/lebesgue_measure.v b/theories/lebesgue_measure.v index 2a1e1993c..9611908ef 100644 --- a/theories/lebesgue_measure.v +++ b/theories/lebesgue_measure.v @@ -1221,7 +1221,7 @@ Lemma measurable_itv_bnd_infty b x : Proof. case: b; first by apply: sub_sigma_algebra; exists x; rewrite set_itv_c_infty. rewrite [X in measurable X](_ : _ = - \bigcup_(k in [set k | k >= x]%N) `[k.+1, +oo[%classic); last first. + \bigcup_(k in [set k | k >= x]%N) `[k.+1, +oo[%classic); last first. apply/seteqP; split => [z /=|/= z [t/= xt]]. rewrite in_itv/= andbT => xz; exists z.-1 => /=. by rewrite -ltnS//=; case: z xz. @@ -1363,7 +1363,7 @@ Lemma measurable_fun_leq D f g : measurable_fun D f -> measurable_fun D g -> Proof. move=> mf mg mD Y mY; have [| | |] := set_bool Y => /eqP ->. - rewrite preimage_true [X in _ `&` X](_ : _ = - \bigcup_(i in range g) ([set y | f y <= i]%O `&` [set t | i <= g t]%O)). + \bigcup_(i in range g) ([set y | f y <= i]%O `&` [set t | i <= g t]%O)). rewrite setI_bigcupr; apply: bigcup_measurable => k fk. rewrite setIIr; apply: measurableI => //. + by rewrite -preimage_itvNyc; exact: mf. @@ -1450,6 +1450,7 @@ Lemma measurable_EFinP d (T : measurableType d) (R : realType) (D : set T) Proof. split=> [mf mD A mA|]; last by move=> mg; exact: measurableT_comp. rewrite [X in measurable X](_ : _ = D `&` (EFin \o g) @^-1` (EFin @` A)). + (* TODO: use measurable_image_EFin? *) by apply: mf => //; exists A => //; exists set0; [constructor|rewrite setU0]. congr (_ `&` _);rewrite eqEsubset; split=> [|? []/= _ /[swap] -[->//]]. by move=> ? ?; exact: preimage_image. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 017e959d3..a6161de94 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -170,11 +170,11 @@ Section bernoulli_trunc. Variables (R : realType). Local Open Scope ring_scope. -Lemma sumbool_ler (x y : R) : {(x <= y)%R} + {(x > y)%R}. +Lemma sumbool_ler (x y : R) : (x <= y)%R + (x > y)%R. Proof. have [_|_] := leP x y. -by apply left. -by apply right. +by apply (*left*) inl. +by apply (*right*) inr. Qed. (* TODO: move? *) @@ -188,11 +188,11 @@ Definition bernoulli0 := @bernoulli R 0%R%:nng ler01. HB.instance Definition _ := Probability.on bernoulli0. Definition bernoulli_trunc (p : R) := match sumbool_ler 0%R p with -| left l0p => match sumbool_ler (NngNum l0p)%:num 1%R with - | left lp1 => [the probability _ _ of @bernoulli R (NngNum l0p) lp1] - | right _ => [the probability _ _ of bernoulli0] +| inl l0p => match sumbool_ler (NngNum l0p)%:num 1%R with + | inl lp1 => [the probability _ _ of @bernoulli R (NngNum l0p) lp1] + | inr _ => [the probability _ _ of bernoulli0] end -| right _ => [the probability _ _ of bernoulli0] +| inr _ => [the probability _ _ of bernoulli0] end. (*HB.instance Definition _ (p : R) := Probability.on (bernoulli_trunc p).*) @@ -204,55 +204,52 @@ Lemma measurable_bernoulli_trunc : Proof. apply: (@measurability _ _ _ _ _ _ (@pset _ _ _ : set (set (pprobability _ R)))) => //. -move=> _ -[_ [r r01] [Ys mYs <-]] <-. -apply: emeasurable_fun_infty_o => //=. +move=> _ -[_ [r r01] [Ys mYs <-]] <-; apply: emeasurable_fun_infty_o => //=. rewrite /bernoulli_trunc/=. have := @subsetT _ Ys; rewrite setT_bool => UT. have [->|->|->|->] /= := subset_set2 UT. -- rewrite [X in measurable_fun _ X](_ : _ = (fun=> 0%E))//. +- rewrite [X in measurable_fun _ X](_ : _ = cst 0%E)//. apply/funext => x/=. by case: sumbool_ler. - rewrite [X in measurable_fun _ X](_ : _ = (fun x => if 0 <= x <= 1 then x%:E else 0%E))//. by apply: measurable_fun_ifT => //=; apply: measurable_and => //; exact: measurable_fun_ler. apply/funext => x/=; case: sumbool_ler => /= x0. case: sumbool_ler => /= x1. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. - by rewrite mem_set//= memNset//= ?simpe x0 x1. - rewrite /bernoulli0. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. - by rewrite mem_set//= memNset//= ?simpe x0/= leNgt x1. - rewrite /bernoulli0. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. - by rewrite mem_set//= memNset//= ?simpe leNgt x0. -- rewrite [X in measurable_fun _ X](_ : _ = (fun x => if 0 <= x <= 1 then (`1-x)%:E else 1%E))//. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. + by rewrite /mscale/= !diracE mem_set//= memNset//= ?simpe x0 x1. + rewrite /bernoulli0 /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. + by rewrite /mscale/= !diracE mem_set//= memNset//= ?simpe x0/= leNgt x1. + rewrite /bernoulli0 /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. + by rewrite /mscale/= !diracE mem_set//= memNset//= ?simpe leNgt x0. +- rewrite [X in measurable_fun _ X](_ : _ = + (fun x => if 0 <= x <= 1 then (`1-x)%:E else 1%E))//. apply: measurable_fun_ifT => //=. by apply: measurable_and => //; exact: measurable_fun_ler. by apply/EFin_measurable_fun; apply/measurable_funB. apply/funext => x/=; case: sumbool_ler => /= x0. case: sumbool_ler => /= x1. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. - by rewrite memNset//= mem_set//= ?simpe x0 x1/=. - rewrite /bernoulli0. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. - by rewrite memNset//= mem_set//= ?simpe x0/= leNgt x1/= onem0. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. + by rewrite /mscale/= !diracE memNset//= mem_set//= ?simpe x0 x1/=. + rewrite /bernoulli0 /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. + by rewrite /mscale/= !diracE memNset//= mem_set//= ?simpe x0/= leNgt x1/= onem0. rewrite /bernoulli0. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. - by rewrite memNset//= mem_set//= leNgt x0/= ?simpe onem0. -- rewrite [X in measurable_fun _ X](_ : _ = (fun=> 1%E))//; apply/funext => x/=. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. + by rewrite /mscale/= !diracE memNset//= mem_set//= leNgt x0/= ?simpe onem0. +- rewrite [X in measurable_fun _ X](_ : _ = cst 1%E)//; apply/funext => x/=. case: sumbool_ler => /= x0. case: sumbool_ler => /= x1. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. - rewrite mem_set//=; last by left. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. + rewrite /mscale/= !diracE mem_set//=; last by left. rewrite mem_set//=; last by right. by rewrite ?simpe -EFinD add_onemK. rewrite /bernoulli0. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. - rewrite mem_set//=; last by left. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. + rewrite /mscale/= !diracE mem_set//=; last by left. rewrite mem_set//=; last by right. by rewrite ?simpe onem0. rewrite /bernoulli0. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//= /mscale/= !diracE. - rewrite mem_set//=; last by left. + rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. + rewrite /mscale/= !diracE mem_set//=; last by left. rewrite mem_set//=; last by right. by rewrite ?simpe onem0. Qed. @@ -263,15 +260,12 @@ Arguments bernoulli_trunc {R}. Arguments measurable_bernoulli_trunc {R}. Lemma integral_bernoulli_trunc {R : realType} (p : R) (f : bool -> \bar R) : - (0 <= p <= 1)%R -> - (forall x, 0 <= f x) -> - \int[bernoulli_trunc p]_y (f y) = - p%:E * f true + (`1-p)%:E * f false. -Proof. -move=> /andP[p0 p1] f0. -rewrite /bernoulli_trunc. -case: (sumbool_ler 0 p) => [p0'/=|]. - case: (sumbool_ler p 1) => [p1'/=|]. + (0 <= p <= 1)%R -> (forall x, 0 <= f x) -> + \int[bernoulli_trunc p]_y (f y) = p%:E * f true + (`1-p)%:E * f false. +Proof. +move=> /andP[p0 p1] f0; rewrite /bernoulli_trunc. +case: sumbool_ler => [? /=|]. + case: (sumbool_ler p 1) => [? /=|]. by rewrite integral_bernoulli. by rewrite ltNge p1. by rewrite ltNge p0. @@ -285,7 +279,7 @@ Local Open Scope ring_scope. Definition bino_term (k : nat) : {nonneg R} := (p%:num^+k * (NngNum (onem_ge0 p1))%:num^+(n-k)%N *+ 'C(n, k))%:nng. -Lemma bino_term0 : +Lemma bino_term0 : bino_term 0 = ((NngNum (onem_ge0 p1))%:num^+n)%:nng. Proof. rewrite /bino_term bin0 subn0/=. @@ -293,7 +287,7 @@ apply/val_inj => /=. by field. Qed. -Lemma bino_term1 : +Lemma bino_term1 : bino_term 1 = (p%:num * (NngNum (onem_ge0 p1))%:num^+(n-1)%N *+ n)%:nng. Proof. rewrite /bino_term bin1/=. @@ -333,49 +327,123 @@ End binomial_probability. Section integral_binomial. Variables (R : realType) (d : measure_display) (T : measurableType d). -Lemma integral_binomial (n : nat) (p : {nonneg R}) +Lemma integral_binomial (n : nat) (p : {nonneg R}) (p1 : (p%:num <= 1)%R) (f : nat -> \bar R) (mf : measurable_fun setT f) : (forall x, 0 <= f x) -> \int[binomial_probability n p1]_y (f y) = \sum_(k < n.+1) (bino_term n p1 k)%:num%:E * f k. Proof. -move=> f0. -rewrite ge0_integral_measure_sum//=; last first. -apply: eq_bigr => i _. -rewrite ge0_integral_mscale//=; last first. -rewrite integral_dirac//=; last first. -by rewrite diracT mul1e. +move=> f0; rewrite ge0_integral_measure_sum//=; apply: eq_bigr => i _. +by rewrite ge0_integral_mscale//= integral_dirac//= diracT mul1e. Qed. End integral_binomial. +(* X + Y is a measurableType if X and Y are *) +HB.instance Definition _ (X Y : pointedType) := + isPointed.Build (X + Y)%type (@inl X Y point). + +Section measurable_sum. +Context d d' (X : measurableType d) (Y : measurableType d'). + +Definition measurable_sum : set (set (X + Y)) := setT. + +Let sum0 : measurable_sum set0. Proof. by []. Qed. + +Let sumC A : measurable_sum A -> measurable_sum (~` A). Proof. by []. Qed. + +Let sumU (F : (set (X + Y))^nat) : (forall i, measurable_sum (F i)) -> + measurable_sum (\bigcup_i F i). +Proof. by []. Qed. + +HB.instance Definition _ := @isMeasurable.Build default_measure_display (X + Y)%type + measurable_sum sum0 sumC sumU. + +End measurable_sum. + +Lemma measurable_fun_sum dA dB d' (A : measurableType dA) (B : measurableType dB) + (Y : measurableType d') (f : A -> Y) (g : B -> Y) : + measurable_fun setT f -> measurable_fun setT g -> + measurable_fun setT (fun tb : A + B => + match tb with inl a => f a | inr b => g b end). +Proof. +move=> mx my/= _ Z mZ /=; rewrite setTI /=. +rewrite (_ : _ @^-1` Z = inl @` (f @^-1` Z) `|` inr @` (g @^-1` Z)). + exact: measurableU. +apply/seteqP; split. + by move=> [a Zxa|b Zxb]/=; [left; exists a|right; exists b]. +by move=> z [/= [a Zxa <-//=]|]/= [b Zyb <-//=]. +Qed. + +(* TODO: measurable_fun_if_pair -> measurable_fun_if_pair_bool? *) +Lemma measurable_fun_if_pair_nat d d' (X : measurableType d) + (Y : measurableType d') (f g : X -> Y) (n : nat) : + measurable_fun setT f -> measurable_fun setT g -> + measurable_fun setT (fun xn => if xn.2 == n then f xn.1 else g xn.1). +Proof. +move=> mx my; apply: measurable_fun_ifT => //=. +- have h : measurable_fun [set: nat] (fun t => t == n) by []. + exact: (@measurableT_comp _ _ _ _ _ _ _ _ _ h). +- exact: measurableT_comp. +- exact: measurableT_comp. +Qed. + Section binomial_trunc. Variables (R : realType). Local Open Scope ring_scope. Definition binomial_probability0 := @binomial_probability R 0 0%:nng%R ler01. -Definition binomial_probability_trunc n (p : R) := - (* (p1 : (p%:num <= 1)%R) := *) +Definition binomial_probability_trunc n (p : R) := match (sumbool_ler 0%R p) with - | left l0p => match (sumbool_ler (NngNum l0p)%:num 1%R) with - | left lp1 => [the probability _ _ of @binomial_probability R n (NngNum l0p) lp1] - | right _ => [the probability _ _ of binomial_probability0] + | inl l0p => match (sumbool_ler (NngNum l0p)%:num 1%R) with + | inl lp1 => [the probability _ _ of @binomial_probability R n (NngNum l0p) lp1] + | inr _ => [the probability _ _ of binomial_probability0] end - | right _ => [the probability _ _ of binomial_probability0] + | inr _ => [the probability _ _ of binomial_probability0] end. -Lemma measurable_binomial_probability_trunc (n : nat) +Lemma measurable_binomial_probability_trunc (n : nat) : measurable_fun setT (binomial_probability_trunc n : R -> pprobability _ _). Proof. apply: (@measurability _ _ _ _ _ _ (@pset _ _ _ : set (set (pprobability _ R)))) => //. -move=> _ -[_ [r r01] [Ys mYs <-]] <-. -apply: emeasurable_fun_infty_o => //=. +move=> _ -[_ [r r01] [Ys mYs <-]] <-; apply: emeasurable_fun_infty_o => //=. rewrite /binomial_probability_trunc/=. -(* have := @subsetT _ Ys; rewrite setT_bool => UT. -have [->|->|->|->] /= := subset_set2 UT. *) -Admitted. +set f := (X in measurable_fun _ X). +rewrite (_ : f = fun x => if 0 <= x <= 1 then (\sum_(m < n.+1) + match sumbool_ler 0 x with + | inl l0p => + match sumbool_ler x 1 with + | inl lp1 => mscale (@bino_term _ n (NngNum l0p) lp1 m) (\d_(nat_of_ord m)) Ys + | inr _ => (x ^+ m * `1-x ^+ (n - m) *+ 'C(n, m))%:E * \d_(nat_of_ord m) Ys + end + | inr _ => (x ^+ m * `1-x ^+ (n - m) *+ 'C(n, m))%:E * \d_(nat_of_ord m) Ys + end)%E + else \d_0%N Ys)//. + move=> ?; apply: measurable_fun_ifT => //=. + by apply: measurable_and => //; exact: measurable_fun_ler. + apply: emeasurable_fun_sum => m /=. + rewrite /mscale/= [X in measurable_fun _ X](_ : _ = (fun x => + ((x ^+ m * `1-x ^+ (n - m) *+ 'C(n, m))%:E * \d_(nat_of_ord m) Ys)%E)); last first. + by apply:funext => x; case: sumbool_ler => // x0; case: sumbool_ler. + apply: emeasurable_funM => //; apply/EFin_measurable_fun => //. + under eq_fun do rewrite -mulrnAr. + apply: measurable_funM => //. + under eq_fun do rewrite -mulr_natr. + apply: measurable_funM => //=. + apply: measurable_fun_pow. + exact: measurable_funB. +rewrite {}/f. +apply/funext => x. +case: sumbool_ler => /= x0. + case: sumbool_ler => /= x1. + by rewrite /binomial_probability/= /msum /= /bino_term/= x0 x1. + rewrite /binomial_probability /= /msum big_ord_recl/= big_ord0 /mscale. + by rewrite /= expr0 mul1r subnn expr0 bin0 mul1e adde0 !leNgt x1 andbF. +rewrite /binomial_probability /= /msum big_ord_recl/= big_ord0 /mscale. +by rewrite /= expr0 mul1r subnn expr0 bin0 mul1e adde0 !leNgt x0/=. +Qed. End binomial_trunc. @@ -386,15 +454,16 @@ Section integral_binomial_trunc. Variables (R : realType) (d : measure_display) (T : measurableType d). Import Notations. -Lemma integral_binomial_probabilty_trunc (n : nat) (p : R) - (p0 : (0 <= p)%R) (p1 : ((NngNum p0)%:num <= 1)%R) (f : mnat -> \bar R) (mf : measurable_fun setT f) : - (forall x, 0 <= f x) -> \int[binomial_probability_trunc n p]_y (f y) = \sum_(k < n.+1) (bino_term n p1 k)%:num%:E * f k. +Lemma integral_binomial_probabilty_trunc (n : nat) (p : R) + (p0 : (0 <= p)%R) (p1 : ((NngNum p0)%:num <= 1)%R) (f : mnat -> \bar R) + (mf : measurable_fun setT f) : (forall x, 0 <= f x) -> + \int[binomial_probability_trunc n p]_y (f y) = + \sum_(k < n.+1) (bino_term n p1 k)%:num%:E * f k. Proof. -move=> f0. -rewrite /binomial_probability_trunc/=. -case: (sumbool_ler 0 p) => [p0'/=|]. - case: (sumbool_ler p 1) => [p1'/=|]. - by rewrite integral_binomial. +move=> f0; rewrite /binomial_probability_trunc/=. +case: sumbool_ler => [? /=|]. + case: sumbool_ler => [?/=|]. + by rewrite integral_binomial. by rewrite ltNge p1. by rewrite ltNge p0. Qed. @@ -406,7 +475,7 @@ Context {R : realType}. Open Scope ring_scope. Lemma binomial3_2 : @binomial_probability R 3 _ (p1S 1) [set 2%N] = (3 / 8)%:E. -Proof. +Proof. rewrite /binomial_probability/msum !big_ord_recl/= big_ord0 adde0 bino_term0. rewrite /mscale/= !diracE /bump/=. repeat rewrite ?binS ?bin0 ?bin1 ?bin_small//. @@ -424,13 +493,11 @@ End binomial_example. Section uniform_probability. Context (R : realType) (a b : R) (ab0 : (0 < b - a)%R). -Definition uniform_probability -(* : set _ -> \bar R *) - := @mscale _ _ R (invr_nonneg (NngNum (ltW ab0))) +Definition uniform_probability (* : set _ -> \bar R *) := + @mscale _ _ R (invr_nonneg (NngNum (ltW ab0))) (mrestr lebesgue_measure (measurable_itv `[a, b])). -(** TODO: set R -> \bar R を書くとMeasure.onが通らない **) -(** **) +(* NB: set R -> \bar R を書くとMeasure.onが通らない *) HB.instance Definition _ := Measure.on uniform_probability. (* Let uniform0 : uniform_probability set0 = 0. @@ -457,6 +524,82 @@ HB.instance Definition _ := @Measure_isProbability.Build _ _ R End uniform_probability. +Section integral_uniform. +Context {R : realType}. + +Let integral_uniform_indic (a b : R) (ab0 : (0 < b - a)%R) E : + measurable E -> let m := uniform_probability ab0 in + \int[m]_x (\1_E x)%:E = + (b - a)^-1%:E * \int[lebesgue_measure]_(x in `[a, b]) (\1_E x)%:E. +Proof. +move=> mE m. +by rewrite !integral_indic//= /uniform_probability/= /mscale/= /mrestr setIT. +Qed. + +Let integral_uniform_nnsfun (f : {nnsfun measurableTypeR R >-> R}) + (a b : R) (ab0 : (0 < b - a)%R) : let m := uniform_probability ab0 in + \int[m]_x (f x)%:E = + (b - a)^-1%:E * \int[lebesgue_measure]_(x in `[a, b]) (f x)%:E. +Proof. +move=> m. +under [LHS]eq_integral do rewrite fimfunE -fsumEFin//. +rewrite [LHS]ge0_integral_fsum//; last 2 first. + - by move=> r; exact/EFin_measurable_fun/measurableT_comp. + - by move=> n x _; rewrite EFinM nnfun_muleindic_ge0. +rewrite -[RHS]ge0_integralZl//; last 3 first. + - exact/EFin_measurable_fun/measurable_funTS. + - by move=> x _; rewrite lee_fin. + - by rewrite lee_fin invr_ge0// ltW. +under [RHS]eq_integral. + move=> x xD; rewrite fimfunE -fsumEFin// ge0_mule_fsumr; last first. + by move=> r; rewrite EFinM nnfun_muleindic_ge0. + over. +rewrite [RHS]ge0_integral_fsum//; last 2 first. + - by move=> r; apply/EFin_measurable_fun; do 2 apply/measurableT_comp => //. + - move=> n x _; rewrite EFinM mule_ge0//; last by rewrite nnfun_muleindic_ge0. + by rewrite lee_fin invr_ge0// ltW. +apply: eq_fsbigr => r _; rewrite ge0_integralZl//. +- by rewrite !integralZl_indic_nnsfun//= integral_uniform_indic// muleCA. +- exact/EFin_measurable_fun/measurableT_comp. +- by move=> t _; rewrite nnfun_muleindic_ge0. +- by rewrite lee_fin invr_ge0// ltW. +Qed. + +Lemma integral_uniform (f : measurableTypeR R -> \bar R) (a b : R) + (ab0 : (0 < b - a)%R) : measurable_fun setT f -> (forall x, 0 <= f x) -> + let m := uniform_probability ab0 in + \int[m]_x f x = + (b - a)^-1%:E * \int[lebesgue_measure]_(x in `[a, b]) f x. +Proof. +move=> mf f0 m. +have [f_ [ndf_ f_f]] := approximation measurableT mf (fun x0 _ => f0 x0). +transitivity (lim (\int[m]_(x in setT) (f_ n x)%:E @[n --> \oo])). + rewrite -monotone_convergence//=. + - apply: eq_integral => ? /[!inE] xD; apply/esym/cvg_lim => //=. + exact: f_f. + - by move=> n; exact/EFin_measurable_fun/measurable_funTS. + - by move=> n ? _; rewrite lee_fin. + - by move=> ? _ ? ? mn; rewrite lee_fin; exact/lefP/ndf_. +rewrite [X in _ = _ * X](_ : _ = lim + (\int[lebesgue_measure]_(x in `[a, b]) (f_ n x)%:E @[n --> \oo])); last first. + rewrite -monotone_convergence//=. + - by apply: eq_integral => ? /[!inE] xD; apply/esym/cvg_lim => //; exact: f_f. + - by move=> n; exact/EFin_measurable_fun/measurable_funTS. + - by move=> n ? _; rewrite lee_fin. + - by move=> ? _ ? ? /ndf_ /lefP; rewrite lee_fin. +rewrite -limeMl//. + apply: congr_lim. + by apply/funext => n /=; exact: integral_uniform_nnsfun. +apply/ereal_nondecreasing_is_cvgn => ? ? ab; apply: ge0_le_integral => //=. +- by move=> ? _; rewrite lee_fin. +- exact/EFin_measurable_fun/measurable_funTS. +- by move=> ? _; rewrite lee_fin. +- exact/EFin_measurable_fun/measurable_funTS. +- by move=> ? _; rewrite lee_fin; move/ndf_ : ab => /lefP. +Qed. + +End integral_uniform. + Section mscore. Context d (T : measurableType d) (R : realType). Variable f : T -> R. @@ -1486,7 +1629,7 @@ Qed. Definition bernoulli_and : R.-sfker T ~> mbool := (letin (sample_cst [the probability _ _ of bernoulli p12]) (letin (sample_cst [the probability _ _ of bernoulli p12]) - (ret (measurable_fun_mand macc1of3 macc2of3)))). + (ret (measurable_fun_mand macc1of3 macc2of3)))). *) Lemma bernoulli_andE t U : @@ -1618,55 +1761,6 @@ Qed. End staton_bus_exponential. -(* X + Y is a measurableType if X and Y are *) -HB.instance Definition _ (X Y : pointedType) := - isPointed.Build (X + Y)%type (@inl X Y point). - -Section measurable_sum. -Context d d' (X : measurableType d) (Y : measurableType d'). - -Definition measurable_sum : set (set (X + Y)) := setT. - -Let sum0 : measurable_sum set0. Proof. by []. Qed. - -Let sumC A : measurable_sum A -> measurable_sum (~` A). Proof. by []. Qed. - -Let sumU (F : (set (X + Y))^nat) : (forall i, measurable_sum (F i)) -> - measurable_sum (\bigcup_i F i). -Proof. by []. Qed. - -HB.instance Definition _ := @isMeasurable.Build default_measure_display (X + Y)%type - measurable_sum sum0 sumC sumU. - -End measurable_sum. - -Lemma measurable_fun_sum dA dB d' (A : measurableType dA) (B : measurableType dB) - (Y : measurableType d') (f : A -> Y) (g : B -> Y) : - measurable_fun setT f -> measurable_fun setT g -> - measurable_fun setT (fun tb : A + B => - match tb with inl a => f a | inr b => g b end). -Proof. -move=> mx my/= _ Z mZ /=; rewrite setTI /=. -rewrite (_ : _ @^-1` Z = inl @` (f @^-1` Z) `|` inr @` (g @^-1` Z)). - exact: measurableU. -apply/seteqP; split. - by move=> [a Zxa|b Zxb]/=; [left; exists a|right; exists b]. -by move=> z [/= [a Zxa <-//=]|]/= [b Zyb <-//=]. -Qed. - -(* TODO: measurable_fun_if_pair -> measurable_fun_if_pair_bool? *) -Lemma measurable_fun_if_pair_nat d d' (X : measurableType d) - (Y : measurableType d') (f g : X -> Y) (n : nat) : - measurable_fun setT f -> measurable_fun setT g -> - measurable_fun setT (fun xn => if xn.2 == n then f xn.1 else g xn.1). -Proof. -move=> mx my; apply: measurable_fun_ifT => //=. -- have h : measurable_fun [set: nat] (fun t => t == n) by []. - exact: (@measurableT_comp _ _ _ _ _ _ _ _ _ h). -- exact: measurableT_comp. -- exact: measurableT_comp. -Qed. - Module CASE_NAT. Section case_nat. Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). From 08e58601f2416457d2e35519cd6d65963e812d09 Mon Sep 17 00:00:00 2001 From: AyumuSaito Date: Tue, 14 Feb 2023 22:02:38 +0900 Subject: [PATCH 15/36] minor fixes Co-authored-by: Reynald Affeldt --- theories/prob_lang.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/theories/prob_lang.v b/theories/prob_lang.v index a6161de94..f329580ad 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -1629,7 +1629,7 @@ Qed. Definition bernoulli_and : R.-sfker T ~> mbool := (letin (sample_cst [the probability _ _ of bernoulli p12]) (letin (sample_cst [the probability _ _ of bernoulli p12]) - (ret (measurable_fun_mand macc1of3 macc2of3)))). + (ret (measurable_fun_mand macc1of3 macc2of3)))). *) Lemma bernoulli_andE t U : From 860a8f004fe23205f8e7a4da487f1bbc5a3caffa Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Tue, 16 Jan 2024 09:44:11 +0900 Subject: [PATCH 16/36] several admits proved --- theories/lang_syntax_examples_wip.v | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/theories/lang_syntax_examples_wip.v b/theories/lang_syntax_examples_wip.v index cae3d8259..43c69487a 100644 --- a/theories/lang_syntax_examples_wip.v +++ b/theories/lang_syntax_examples_wip.v @@ -301,7 +301,9 @@ congr (_ * _)%E. apply: eq_integral => t t01. apply: s01. by rewrite inE in t01. -Qed. +admit. +admit. +Admitted. (* Lemma casino01 : execP casino0 = execP casino1. Proof. From 0a228fc09a120db646f91e52e88c414b7a2559a1 Mon Sep 17 00:00:00 2001 From: AyumuSaito Date: Fri, 19 Jan 2024 08:45:23 +0900 Subject: [PATCH 17/36] definition beta - define beta_probability - proof beta_ge0 --- theories/lang_syntax.v | 16 ++- theories/lang_syntax_examples_wip.v | 205 +++++++++------------------- theories/prob_lang.v | 99 +++++++++++++- 3 files changed, 168 insertions(+), 152 deletions(-) diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index 1e12682e2..41f094f32 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -451,6 +451,7 @@ Inductive exp : flag -> ctx -> typ -> Type := | exp_binomial_trunc g (n : nat) : exp D g Real -> exp D g (Prob Nat) | exp_uniform g (a b : R) (ab0 : (0 < b - a)%R) : exp D g (Prob Real) +| exp_beta g (a b : nat) : exp D g (Prob Real) | exp_poisson g : nat -> exp D g Real -> exp D g Real | exp_normalize g t : exp P g t -> exp D g (Prob t) | exp_letin g t1 t2 str : exp P g t1 -> exp P ((str, t1) :: g) t2 -> @@ -486,6 +487,7 @@ Arguments exp_bernoulli {R g}. Arguments exp_bernoulli_trunc {R g} &. Arguments exp_binomial {R g}. Arguments exp_uniform {R g} &. +Arguments exp_beta {R g} &. Arguments exp_binomial_trunc {R g} &. Arguments exp_poisson {R g}. Arguments exp_normalize {R g _}. @@ -575,6 +577,7 @@ Fixpoint free_vars k g t (e : @exp R k g t) : seq string := | exp_bernoulli_trunc _ e => free_vars e | exp_binomial _ _ _ _ => [::] | exp_uniform _ _ _ _ => [::] + | exp_beta _ _ _ => [::] | exp_binomial_trunc _ _ e => free_vars e | exp_poisson _ _ e => free_vars e | exp_normalize _ _ e => free_vars e @@ -755,6 +758,9 @@ Inductive evalD : forall g t, exp D g t -> (exp_uniform a b ab0 : exp D g _) -D> cst (uniform_probability ab0) ; measurable_cst _ +| eval_beta g (a b : nat) (p : {nonneg R}) (p1 : (p%:num <= 1)%R) : + (exp_beta a b : exp D g _) -D> cst (beta a b p1) ; measurable_cst _ + | eval_poisson g n (e : exp D g _) f mf : e -D> f ; mf -> exp_poisson n e -D> poisson n \o f ; @@ -902,11 +908,11 @@ all: (rewrite {g t e u v mu mv hu}). inversion 1; subst g0 a0 b0. inj_ex H4; subst v. by have -> : ab0 = ab2. -- move=> g n e0 f mf ev IH {}v {}mv. - inversion 1; subst g0 n0. - inj_ex H2; subst e0. - inj_ex H4; subst v. - by rewrite (IH _ _ H3). +- move=> g a b p p1 {}v {}mv. + inversion 1. subst g0 a0 b0. + inj_ex H2; subst v. + inj_ex H4. + have -> : p1 = p2 by []. - move=> g t e0 k ev IH {}v {}mv. inversion 1; subst g0 t0. inj_ex H2; subst e0. diff --git a/theories/lang_syntax_examples_wip.v b/theories/lang_syntax_examples_wip.v index 43c69487a..426d90091 100644 --- a/theories/lang_syntax_examples_wip.v +++ b/theories/lang_syntax_examples_wip.v @@ -8,6 +8,10 @@ Require Import lebesgue_measure numfun lebesgue_integral kernel prob_lang. Require Import lang_syntax_util lang_syntax. From mathcomp Require Import ring lra. +(******************************************************************************) +(* Casino example *) +(* some steps *) + Set Implicit Arguments. Unset Strict Implicit. Unset Printing Implicit Defensive. @@ -19,12 +23,64 @@ Local Open Scope classical_set_scope. Local Open Scope ring_scope. (* Local Open Scope ereal_scope. *) +Section trunc_lemmas. +Open Scope ring_scope. +Open Scope lang_scope. +Context (R : realType). + +Lemma bernoulli_truncE (p : R) U : + (0 <= p <= 1)%R -> + (bernoulli_trunc p U = + p%:E * \d_true U + (`1-p)%:E * \d_false U)%E. +Proof. +move=> /andP[p0 p1]. +rewrite /bernoulli_trunc. +case: (sumbool_ler 0 p) => [{}p0/=|]. + case: (sumbool_ler p 1) => [{}p1/=|]. + by rewrite /bernoulli/= measure_addE. + by rewrite ltNge p1. +by rewrite ltNge p0. +Qed. + +Lemma __ p p1 : + @execD R [::] _ (exp_bernoulli p p1) = + execD (exp_bernoulli_trunc [{p%:num}:R]). +Proof. +apply: eq_execD. +rewrite execD_bernoulli execD_bernoulli_trunc execD_real/=. +apply: funext=> x /=. +rewrite /bernoulli_trunc. +case: (sumbool_ler 0 p%:num) => [{}p0/=|]. + case: (sumbool_ler p%:num 1) => [{}p1'/=|]. + admit. +Abort. + + +End trunc_lemmas. + Section casino_example. Open Scope ring_scope. Open Scope lang_scope. Context (R : realType). Lemma a01 : 0 < 1 - 0 :> R. Proof. by []. Qed. +(* guard test *) +Definition test_guard : @exp R _ [::] _ := [ + let "p" := Sample {exp_bernoulli (1 / 3)%:nng (p1S 2)} in + let "_" := if #{"p"} then return TT else Score {0}:R in + return #{"p"} +]. + +Lemma exec_guard t U : execP test_guard t U = ((1 / 3)%:E * \d_true U)%E. +Proof. +rewrite /test_guard 2!execP_letin execP_sample execD_bernoulli execP_if/=. +rewrite !execP_return !exp_var'E !(execD_var_erefl "p") execD_unit execP_score execD_real/=. +rewrite letin'E ge0_integral_measure_sum//. +rewrite !big_ord_recl big_ord0 !ge0_integral_mscale//= !integral_dirac//. +rewrite !letin'E !iteE/= integral_dirac// ge0_integral_mscale//=. +by rewrite normr0 mul0e !mule0 !adde0 !diracT !mul1e. +Qed. + (* Definition ex : exp _ [::] _ := @exp_bernoulli R [::] (1 / 2)%:nng (p1S 1). Example ex1 : projT1 (execD ex) tt = 1%:E. *) @@ -60,20 +116,6 @@ Definition casino1' : @exp R _ [::] _ := let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. -Lemma bernoulli_truncE (p : R) U : - (0 <= p <= 1)%R -> - (bernoulli_trunc p U = - p%:E * \d_true U + (`1-p)%:E * \d_false U)%E. -Proof. -move=> /andP[p0 p1]. -rewrite /bernoulli_trunc. -case: (sumbool_ler 0 p) => [{}p0/=|]. - case: (sumbool_ler p 1) => [{}p1/=|]. - by rewrite /bernoulli/= measure_addE. - by rewrite ltNge p1. -by rewrite ltNge p0. -Qed. - Lemma binomial_le1' n p U : 0 <= p <= 1 -> (\int[binomial_probability_trunc n p]_y0 \d_(0 < y0)%N U = @@ -170,94 +212,6 @@ exact: p0. apply/onem_ge0/p1. Qed. -(* exec [let x := sample (binomial n e) in *) -(* return (x >= 1)] = *) -(* exec [let y := sample (bernoulli (1 - e^n)) in *) -(* return y] *) -(* Lemma binomial_le1' x y n g e t U : - (0 <= (projT1 (execD e) t : mtyp Real) <= 1) -> - execP ([let x := Sample {exp_binomial_trunc n e} in - return {1}:N <= #x] : @exp R _ g _) t U = - execP [let y := Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - e]} ^+ n]} in - return #y] t U. -Proof. -rewrite !execP_letin !execP_sample execD_binomial_trunc execD_bernoulli_trunc/=. -rewrite !exp_var'E/=. - exact/ctx_prf_head. - exact/ctx_prf_head. -move=> H0 H1. -rewrite !execP_return !execD_rel. - have /= := @execD_var R ((y, Bool) :: g) y. - rewrite eqxx => /(_ H0) ->. - have /= := @execD_var R ((x, Nat) :: g) x. - rewrite eqxx => /(_ H1) -> /=. -rewrite (@execD_bin _ _ binop_minus) execD_pow (@execD_bin _ _ binop_minus). -rewrite !execD_real execD_nat/=. -rewrite 2!letin'E/=. -set p := projT1 (execD e) t. -move => /andP[p0 p1]. -rewrite (@integral_bernoulli_trunc _ _ (fun x => \d_x U))//; last first. - apply/andP; split. - apply/onemX_ge0; lra. - apply/onem_le1/exprn_ge0; lra. -rewrite (@integral_binomial_probabilty_trunc _ n p _ _ (fun y => \d_(1 <= y)%N U))//; last first. - (* apply: measurable_fun_dirac. - have := @subsetT _ U; rewrite setT_bool => UT. - have [->|->|->|->] /= := subset_set2 UT. - exact: measurable0. - rewrite [X in measurable X](_ : _ = `[1, +oo[%classic) //. - apply/seteqP. - admit. - rewrite [X in measurable X](_ : _ = `]-oo, 1[%classic) //. - apply/seteqP. - admit. - admit. - rewrite diracE. *) -rewrite !big_ord_recl/=. -rewrite addeC /bump/=. -congr (_ + _)%E. - under eq_bigr => i _. - have -> : (0 < 1 + i)%N => //. - over. -congr (_ * _)%E. -have -> : (0 < 0)%N = false. -rewrite /bump. -under eq_bigr => i _. - rewrite /= natrD. - have -> : 1 <= 1 + i%:R :> R. - by rewrite lerDl. - over. -rewrite addeC -ge0_sume_distrl. - congr (_ + _)%E; congr (_ * _)%E. - have -> : (\sum_(i < n) (p ^+ (n - (1 + i)) * `1-p ^+ (1 + i) *+ 'C(n, 1 + i))%:E)%E = - (\sum_(i < n.+1) (p ^+ (n - i) * `1-p ^+ i *+ 'C(n, i))%:E - (p ^+ n)%:E)%E. - rewrite big_ord_recl/= subn0 addeC addeA. - rewrite bin0 mulr1 mulr1n. - have <- : 0%E = ((- p ^+ n)%:E + (p ^+ n)%:E)%E. - rewrite EFinN. - congr _%:E. - lra. - by rewrite add0e. - congr _%E. - rewrite sumEFin. - rewrite !EFinB EFin_expe. - congr (_ - _)%E. - rewrite -(@exprDn_comm _ p `1-p n); last first. - by rewrite /GRing.comm/onem; lra. - rewrite /onem addrC. - have -> : 1 - p + p = 1 by lra. - by rewrite expr1n. - rewrite subn0 expr0 bin0 mulr1 mulr1n. - rewrite /onem. - congr _%:E. - set pn := p ^+ n. - lra. -move=> i _. -apply/mulrn_wge0/mulr_ge0; apply/exprn_ge0. -exact: p0. -apply/onem_ge0/p1. -Admitted. *) - Lemma __ : uniform_probability a01 `[0, (1 / 2)] = (1 / 2)%:E. Proof. rewrite /uniform_probability /mscale/= /mrestr. @@ -284,21 +238,18 @@ Qed. Let weak_head fl g {t1 t2} x (e : @exp R fl g t2) (xg : x \notin dom g) := exp_weak fl [::] _ (x, t1) e xg. -Lemma execP_letin_uniform g u p - (* (Hap : infer (a != p)) (Hbp : infer (b != p)) *) - (* (bg : b \notin dom ((p, Real) :: g)) (ag : b \notin dom ((b, Nat) :: (p, Real) :: g)) *) - (s0 s1 : exp P ((p, Real) :: g) u) : - (forall (t : R) x U, 0 <= t <= 1 -> - execP s0 (t, x) U = execP s1 (t, x) U) -> +Lemma execP_letin_uniform g t str (s0 s1 : exp P ((str, Real) :: g) t) : + (forall (p : R) x U, 0 <= p <= 1 -> + execP s0 (p, x) U = execP s1 (p, x) U) -> forall x U, measurable U -> - execP [let p := Sample {@exp_uniform _ g 0 1 a01} in {s0}] x U = - execP [let p := Sample {@exp_uniform _ g 0 1 a01} in {s1}] x U. + execP [let str := Sample {@exp_uniform _ g 0 1 a01} in {s0}] x U = + execP [let str := Sample {@exp_uniform _ g 0 1 a01} in {s1}] x U. Proof. move=> s01 x U mU. rewrite !execP_letin execP_sample execD_uniform/=. rewrite !letin'_sample_uniform//. congr (_ * _)%E. -apply: eq_integral => t t01. +apply: eq_integral => p p01. apply: s01. by rewrite inE in t01. admit. @@ -360,21 +311,10 @@ Qed. *) Lemma casino01' y V : measurable V -> execP casino0 y V = execP casino1' y V. Proof. -move=> mV. +move=> mV //. rewrite /casino0 /casino1. -pose s0 := - [let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in - let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in - let "a2" := Sample {exp_binomial_trunc 3 [#{"p"}]} in - return {1}:N <= #{"a2"}]. -(* pose s1 := - [let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in - let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in - return {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%N}]}]. *) -have := (@execP_letin_uniform [::] Bool "p" (s0 R (found "p" Real [::]) _ _) _). -apply => //. +apply: execP_letin_uniform => //. move=> p x U r01. -rewrite /s0/=. rewrite !execP_letin !execP_sample !execD_binomial_trunc /=. rewrite execP_if execP_score !execP_return !execD_bernoulli_trunc/=. rewrite !execD_rel (@execD_bin _ _ binop_minus) execD_pow. @@ -394,23 +334,6 @@ rewrite !letin'E/=. by apply/binomial_le1'/andP. Qed. -(* guard test *) -Definition test_guard : @exp R _ [::] _ := [ - let "p" := Sample {exp_bernoulli (1 / 3)%:nng (p1S 2)} in - let "_" := if #{"p"} then return TT else Score {0}:R in - return #{"p"} -]. - -Lemma exec_guard t U : execP test_guard t U = ((1 / 3)%:E * \d_true U)%E. -Proof. -rewrite /test_guard 2!execP_letin execP_sample execD_bernoulli execP_if/=. -rewrite !execP_return !exp_var'E !(execD_var_erefl "p") execD_unit execP_score execD_real/=. -rewrite letin'E ge0_integral_measure_sum//. -rewrite !big_ord_recl big_ord0 !ge0_integral_mscale//= !integral_dirac//. -rewrite !letin'E !iteE/= integral_dirac// ge0_integral_mscale//=. -by rewrite normr0 mul0e !mule0 !adde0 !diracT !mul1e. -Qed. - Lemma exec_casino t U : execP casino0 t U = ((10 / 99)%:E * \d_true U + (1 / 99)%:E * \d_false U)%E . Proof. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index f329580ad..04897011a 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -190,9 +190,9 @@ HB.instance Definition _ := Probability.on bernoulli0. Definition bernoulli_trunc (p : R) := match sumbool_ler 0%R p with | inl l0p => match sumbool_ler (NngNum l0p)%:num 1%R with | inl lp1 => [the probability _ _ of @bernoulli R (NngNum l0p) lp1] - | inr _ => [the probability _ _ of bernoulli0] + | inr _ => bernoulli0 end -| inr _ => [the probability _ _ of bernoulli0] +| inr _ => bernoulli0 end. (*HB.instance Definition _ (p : R) := Probability.on (bernoulli_trunc p).*) @@ -275,7 +275,7 @@ Section binomial_probability. Context {R : realType} (n : nat) (p : {nonneg R}) (p1 : (p%:num <= 1)%R). Local Open Scope ring_scope. -(* C(n, k) * p^(n-k) * (1-p)^k *) +(* C(n, k) * p^k * (1-p)^(n-k) *) Definition bino_term (k : nat) : {nonneg R} := (p%:num^+k * (NngNum (onem_ge0 p1))%:num^+(n-k)%N *+ 'C(n, k))%:nng. @@ -301,9 +301,7 @@ Import Notations. [the measure _ _ of \d_k]]). *) (* \sum_(k < n.+1) (bino_coef p n k) * \d_k. *) Definition binomial_probability : set nat -> \bar R := - @msum _ _ R - (fun k => [the measure _ _ of mscale (bino_term k) - [the measure _ _ of \d_k]]) n.+1. + @msum _ _ R (fun k => mscale (bino_term k) \d_k) n.+1. HB.instance Definition _ := Measure.on binomial_probability. @@ -600,6 +598,95 @@ Qed. End integral_uniform. +Section beta_probability. +Context {R : realType}. +Local Open Scope ring_scope. +Variables (a b : nat). + +Definition Beta t : R := + t^+(a-1) * (1-t)^+(b-1). + +Definition B : R := + (a-1)`!%:R * (b-1)`!%:R / (a+b-1)`!%:R. + (* \int[lebesgue_measure]_(t in `[0%R, 1%R]) Beta t. *) + +Axiom beta1 : \int[lebesgue_measure]_(x in `[0, 1]) (x^+(a-1) * (1-x)^+(b-1)) = B. + +(* apply: fine_ge0. +apply: integral_ge0 => x x01. +have /andP[x0 x1] : (0 <= x <= 1) by apply: x01. +rewrite lee_fin. +apply: mulr_ge0; apply/exprn_ge0; lra. *) + +Set Printing All. + +Lemma beta_ge0 : 0 <= B. +Proof. +rewrite /B. +Admitted. + +(* Definition betaPDF (p : R) := + if 0 <= p <= 1 then Beta p / B else 0. + +Lemma measurable_beta : measurable_fun setT betaPDF. +Proof. +rewrite /betaPDF; apply: measurable_fun_if => //. + apply: (measurable_fun_bool true). + by rewrite (_ : _ @^-1` _ = `[0, 1]%classic). +apply: measurable_funM=> //; apply /measurable_funM => //. +apply/measurable_fun_pow/measurable_funB => //. +Qed. *) + +(* TODO: beta_ge0 a b : is_true (0 <= beta a b) (to remove %E) *) +Let p01 : 0 < 1 - 0 :> R. +Proof. lra. Qed. + +(* 1/B(a, b) * \int_U p^(a-1) * (1-p)^(b-1) dx = beta *) +Definition beta (U : set _) : \bar R := + (1 / B)%:E * \int[uniform_probability p01]_(t in U) (t^+(a-1) * (1-t)^+(b-1))%:E. + (* \int[lebesgue_measure]_(t in U) + @mscale _ _ R (t^+(a-1) + (* * (NngNum (onem_ge0 p1))%:num^+(b-1) *) + * + (invr_nonneg (NngNum beta_ge0))%:num)%:nng + (mrestr lebesgue_measure (measurable_itv `[0, 1])) U. *) + +Example __ : beta `[0, 1] = 1%:E. +Proof. +rewrite /beta integral_mkcond. +rewrite integral_uniform//=. +rewrite oppr0 addr0 invr1 mul1e. +Admitted. + +HB.instance Definition _ (p : {nonneg R}) (p1 : p%:num <= 1) + := Measure.on (beta p1). + +Let beta_setT (p : {nonneg R}) (p1 : p%:num <= 1) + : beta p1 [set: _] = 1%:E. +Proof. +rewrite /beta /mscale/= /mrestr/=. +rewrite setTI lebesgue_measure_itv/= lte_fin. +rewrite ltr01 oppr0 adde0 mule1 /B /Beta. +(* rewrite -subr_gt0 -EFinD . -EFinM mulVf// gt_eqF// subr_gt0. *) +Admitted. + +HB.instance Definition _ (p : {nonneg R}) (p1 : p%:num <= 1) := @Measure_isProbability.Build _ _ R + (beta p1) (beta_setT p1). + +(* Lemma __ : beta_probability 6 4 (p1S 2) `[0, 1] = 1%:E. +Proof. +rewrite /beta_probability/beta/= /mscale/=. +rewrite lebesgue_measure_itv. *) + +Let H01 : (0 < 1 - 0 :> R). +Proof. lra. Qed. + +(* Lemma uniform_beta U : (\int[uniform_probability H01]_x + \int[mscale (NngNum (normr_ge0 (56 * x ^+ 5 * (1 - x) ^+ 3)%R)) (measure_dirac__canonical__measure_Measure tt R)]__ + bernoulli_trunc (1 - (1 - x) ^+ 3) U)%E = 1%:E. *) + +End beta_probability. + Section mscore. Context d (T : measurableType d) (R : realType). Variable f : T -> R. From ee3c63872394a13ac04213316573cc94c5b49d8a Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Thu, 15 Feb 2024 13:21:17 +0900 Subject: [PATCH 18/36] fix --- theories/lang_syntax_examples_wip.v | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/theories/lang_syntax_examples_wip.v b/theories/lang_syntax_examples_wip.v index 426d90091..4edd34526 100644 --- a/theories/lang_syntax_examples_wip.v +++ b/theories/lang_syntax_examples_wip.v @@ -251,10 +251,8 @@ rewrite !letin'_sample_uniform//. congr (_ * _)%E. apply: eq_integral => p p01. apply: s01. -by rewrite inE in t01. -admit. -admit. -Admitted. +by rewrite inE in p01. +Qed. (* Lemma casino01 : execP casino0 = execP casino1. Proof. From 3f784eae6db79bbe79c88e76d981958931e89916 Mon Sep 17 00:00:00 2001 From: AyumuSaito Date: Fri, 16 Feb 2024 10:48:05 +0900 Subject: [PATCH 19/36] write casino (wip) - add exp_beta --- theories/lang_syntax.v | 108 ++++++++++++++++++++++++---- theories/lang_syntax_examples_wip.v | 60 ++++++++++++++++ theories/prob_lang.v | 55 +++++++++++--- 3 files changed, 201 insertions(+), 22 deletions(-) diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index 41f094f32..40a8483a1 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -346,6 +346,75 @@ End accessor_functions. Arguments acc_typ {R} s n. Arguments measurable_acc_typ {R} s n. + +Section beta. +Context {R : realType}. +(* Check sample_cst (beta 6 4) : R.-sfker _ ~> R. *) +(* Check sample_cst (beta 6 4) : R.-ker _ ~> measurableTypeR R. *) +Check sample_cst (uniform_probability _) : R.-ker _ ~> measurableTypeR R. + +Open Scope ring_scope. +Lemma a01 : 0 < 1 - 0 :> R. Proof. by []. Qed. +Import Notations. + +(* Lemma beta11_uniform : + beta 1 1 `[0, 1] = uniform_probability a01 `[0, 1]. +Proof. +rewrite /beta /uniform_probability. +congr mscale. +congr invr_nonneg. +admit. +rewrite /mscale. +apply: funext=> x. *) + +Definition beta_bern : R.-sfker munit ~> mbool := + @letin' _ _ _ munit _ mbool R + (sample_cst (beta 6 4)) + (* (sample_cst (uniform_probability a01)) *) + (sample (bernoulli_trunc \o (@fst (measurableTypeR R) _)) (measurableT_comp measurable_bernoulli_trunc (measurable_acc_typ [:: Real] 0))). + +Lemma letin'_sample_uniform d d' (T : measurableType d) + (T' : measurableType d') (a b : R) (ab0 : (0 < b - a)%R) + (u : R.-sfker [the measurableType _ of (_ * T)%type] ~> T') x y : + measurable y -> + letin' (sample_cst (uniform_probability ab0)) u x y = + ((b - a)^-1%:E * \int[lebesgue_measure]_(x0 in `[a, b]) u (x0, x) y)%E. +Admitted. + +Definition uni_bern : R.-sfker munit ~> mbool := + @letin' _ _ _ munit (measurableTypeR R) mbool R + (sample_cst (uniform_probability a01)) + (* (sample_cst (uniform_probability a01)) *) + (sample (bernoulli_trunc \o (@fst (measurableTypeR R) _)) (measurableT_comp measurable_bernoulli_trunc (measurable_acc_typ [:: Real] 0))). + +Lemma ex_beta_bern : beta_bern tt [set true] = (6 / 10)%:E. +Proof. +rewrite /beta_bern /uni_bern. +rewrite [LHS]letin'E. +rewrite /beta. +rewrite ge0_integral_mscale//. +rewrite /mscale/=/B. +rewrite /prebeta/=. +Search integral lebesgue_measure. + +(* rewrite ge0_integral_mscale//=. +rewrite EFinM. +congr (_ * _)%E. +rewrite /prebeta/=. +Search "pow". +rewrite subn1/=. +transitivity (\int[(integral (uniform_probability p01))^~ (@cst R _ 1%:E)]_x bernoulli_trunc x U)%E. + admit. +rewrite /bernoulli_trunc/=. +rewrite integral_bernoulli_trunc. +(* rewrite /B invr1 !mulr1 fact0 invr1 mul1e. *) +rewrite /prebeta/=. +under eq_integral. +Search integral lebesgue_measure. *) +Abort. + +End beta. + Section context. Variables (R : realType). Definition ctx := seq (string * typ). @@ -451,7 +520,7 @@ Inductive exp : flag -> ctx -> typ -> Type := | exp_binomial_trunc g (n : nat) : exp D g Real -> exp D g (Prob Nat) | exp_uniform g (a b : R) (ab0 : (0 < b - a)%R) : exp D g (Prob Real) -| exp_beta g (a b : nat) : exp D g (Prob Real) +| exp_beta g (a b : nat) (* NB: shound be R *) : exp D g (Prob Real) | exp_poisson g : nat -> exp D g Real -> exp D g Real | exp_normalize g t : exp P g t -> exp D g (Prob t) | exp_letin g t1 t2 str : exp P g t1 -> exp P ((str, t1) :: g) t2 -> @@ -758,8 +827,8 @@ Inductive evalD : forall g t, exp D g t -> (exp_uniform a b ab0 : exp D g _) -D> cst (uniform_probability ab0) ; measurable_cst _ -| eval_beta g (a b : nat) (p : {nonneg R}) (p1 : (p%:num <= 1)%R) : - (exp_beta a b : exp D g _) -D> cst (beta a b p1) ; measurable_cst _ +| eval_beta g (a b : nat) : + (exp_beta a b : exp D g _) -D> cst (beta a b) ; measurable_cst _ | eval_poisson g n (e : exp D g _) f mf : e -D> f ; mf -> @@ -896,7 +965,7 @@ all: (rewrite {g t e u v mu mv hu}). by rewrite (IH _ _ H2). - move=> g n p p1 {}v {}mv. inversion 1; subst g0 n0 p0. - inj_ex H4; subst v. + inj_ex H2; subst v. by have -> : p1 = p3 by []. - move=> g n e f mf ev IH {}v {}mv. inversion 1; subst g0 n0. @@ -906,17 +975,21 @@ all: (rewrite {g t e u v mu mv hu}). by rewrite (IH _ _ H3). - move=> g a b ab0 {}v {}mv. inversion 1; subst g0 a0 b0. - inj_ex H4; subst v. + inj_ex H2; subst v. by have -> : ab0 = ab2. -- move=> g a b p p1 {}v {}mv. +- move=> g a b {}v {}mv. inversion 1. subst g0 a0 b0. - inj_ex H2; subst v. - inj_ex H4. - have -> : p1 = p2 by []. -- move=> g t e0 k ev IH {}v {}mv. - inversion 1; subst g0 t0. + by inj_ex H2; subst v. (* TODO: beta *) +- move=> g t e k mk ev IH {}v {}mv. + inversion 1; subst g0 t. inj_ex H2; subst e0. inj_ex H4; subst v. + by rewrite (IH _ _ H3). +- move=> g t e k ev IH f mf. + inversion 1; subst g0 t0. + inj_ex H2; subst e0. + inj_ex H4; subst f. + inj_ex H5; subst mf. by rewrite (IH _ H3). - move=> g t e f mf e1 f1 mf1 e2 f2 mf2 ev ih ev1 ih1 ev2 ih2 v m. inversion 1; subst g0 t0. @@ -1050,7 +1123,7 @@ all: rewrite {g t e u v eu}. by rewrite (IH _ _ H2). - move=> g n p p1 {}v {}mv. inversion 1; subst g0 n0 p0. - inj_ex H4; subst v. + inj_ex H2; subst v. by have -> : p1 = p3 by []. - move=> g n e f mf ev IH {}v {}mv. inversion 1; subst g0 n0. @@ -1060,8 +1133,11 @@ all: rewrite {g t e u v eu}. by rewrite (IH _ _ H3). - move=> g a b ab0 {}v {}mv. inversion 1; subst g0 a0 b0. - inj_ex H4; subst v. + inj_ex H2; subst v. by have -> : ab0 = ab2. +- move=> g a b {}v {}mv. + inversion 1; subst g0 a0 b0. + by inj_ex H2; subst v. - move=> g n e f mf ev IH {}v {}mv. inversion 1; subst g0 n0. inj_ex H2; subst e0. @@ -1163,6 +1239,7 @@ all: rewrite {z g t}. exists (binomial_probability_trunc n \o p). eexists; exact: (eval_binomial_trunc n). - by eexists; eexists; exact: eval_uniform. +- by eexists; eexists; exact: eval_beta. - move=> g h e [f [mf H]]. by exists (poisson h \o f); eexists; exact: eval_poisson. - move=> g t e [k ek]. @@ -1358,6 +1435,11 @@ Lemma execD_uniform g a b ab0 : existT _ (cst [the probability _ _ of uniform_probability ab0]) (measurable_cst _). Proof. exact/execD_evalD/eval_uniform. Qed. +Lemma execD_beta g a b : + @execD g _ (exp_beta a b) = + existT _ (cst [the probability _ _ of beta a b]) (measurable_cst _). +Proof. exact/execD_evalD/eval_beta. Qed. + Lemma execD_normalize_pt g t (e : exp P g t) : @execD g _ [Normalize e] = existT _ (normalize_pt (execP e) : _ -> pprobability _ _) diff --git a/theories/lang_syntax_examples_wip.v b/theories/lang_syntax_examples_wip.v index 4edd34526..d96074b1c 100644 --- a/theories/lang_syntax_examples_wip.v +++ b/theories/lang_syntax_examples_wip.v @@ -414,4 +414,64 @@ congr (_%:E * _)%E. lra. Qed. +Definition casino2 : @exp R _ [::] _ := + [let "p" := Sample {exp_uniform 0 1 a01} in + let "_" := Score {[{56}:R * #{"p"} ^+ {5%nat} * {[{1}:R - #{"p"}]} ^+ {3%nat}]} in + Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. + +Lemma casino12 y V : + measurable V -> + execP casino1' y V = execP casino2 y V. +Proof. +move=> mV. +rewrite /casino1' /casino2. +apply: execP_letin_uniform => //. +move=> p x U /andP[p0 p1]. +rewrite !execP_letin !execP_sample execP_if execD_rel/=. +rewrite !execP_score !(@execD_bin _ _ binop_mult). +rewrite !execD_bernoulli_trunc/= !(@execD_bin _ _ binop_minus) !execD_pow. +rewrite !(@execD_bin _ _ binop_minus)/=. +rewrite !execD_real !execD_nat/= execP_return execD_unit. +rewrite !execD_binomial_trunc/=. +rewrite !exp_var'E !(execD_var_erefl "p") !(execD_var_erefl "a1")/=. +rewrite !letin'E/=. +rewrite integral_binomial_probabilty_trunc//=. +(* set s := letin' _ _. *) +rewrite (bigD1 (inord 5))//=. + rewrite big1; last first. + move=> [[|[|[|[|[|[|[|[|[|//]]]]]]]]]]//= Hi Hi5; rewrite letin'E iteE; + rewrite ?ge0_integral_mscale//= ?normr0 ?mul0e ?mule0 ?add0e//. + suff: false by []. + move/negbTE: Hi5 => <-. + by apply/eqP/val_inj => /=; rewrite inordK. +rewrite letin'E iteE ge0_integral_mscale//= inordK//= adde0 /onem. +congr (_ * _)%E. +rewrite ger0_norm. + by rewrite -mulrA mulr_natl. +apply/mulr_ge0. + exact/mulr_ge0/exprn_ge0. +apply/exprn_ge0. +by rewrite subr_ge0. +Qed. + +Definition casino3 : @exp R _ [::] _ := + [let "_" := Score {1 / 9}:R in + let "p" := Sample {exp_beta 6 4} in + Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. + +Definition casino4 : @exp R _ [::] _ := + [let "_" := Score {1 / 9}:R in + Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. + +Definition casino5 : @exp R _ [::] _ := + [Normalize Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. + +(* Lemma casino5_ans U : projT1 (execD casino5) tt U = + ((10 / 11)%:E * \d_true U + (1 / 11)%:E * \d_false U)%E. +Proof. +rewrite /casino5 execD_normalize_pt execP_sample execD_bernoulli_trunc/=. +rewrite execD_real/=. +rewrite normalizeE/= bernoulli_truncE. +rewrite /mnormalize/=. *) + End casino_example. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 04897011a..4fa094059 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -620,7 +620,7 @@ apply: mulr_ge0; apply/exprn_ge0; lra. *) Set Printing All. -Lemma beta_ge0 : 0 <= B. +Lemma B_ge0 : 0 <= B. Proof. rewrite /B. Admitted. @@ -638,12 +638,39 @@ apply/measurable_fun_pow/measurable_funB => //. Qed. *) (* TODO: beta_ge0 a b : is_true (0 <= beta a b) (to remove %E) *) -Let p01 : 0 < 1 - 0 :> R. -Proof. lra. Qed. +Definition p01 : 0 < 1 - 0 :> R. +Proof. by []. Qed. + +Definition prebeta U : \bar R := + (* \int[lebesgue_measure]_(x in U `&` `[0, 1]) (x^+(a-1) * (1-x)^+(b-1))%:E. *) + \int[uniform_probability p01]_(t in U) (t^+(a-1) * (1-t)^+(b-1))%:E. + +Let prebeta0 : prebeta set0 = 0%:E. +Proof. +(* by rewrite /prebeta integral_setI_indic// integral_set0. *) +by rewrite /prebeta integral_set0. +Qed. + +Let prebeta_ge0 U : (0 <= prebeta U)%E. +Proof. +rewrite /prebeta. +rewrite integral_ge0// => x Ux. +Admitted. + +Let prebeta_sigma_additive : semi_sigma_additive prebeta. +Proof. move=> /= F mF tF mUF. +rewrite /prebeta. +Admitted. + +HB.instance Definition _ := isMeasure.Build _ _ _ prebeta + prebeta0 prebeta_ge0 prebeta_sigma_additive. + +(* HB.instance Definition _ := Measure.on prebeta. *) (* 1/B(a, b) * \int_U p^(a-1) * (1-p)^(b-1) dx = beta *) -Definition beta (U : set _) : \bar R := - (1 / B)%:E * \int[uniform_probability p01]_(t in U) (t^+(a-1) * (1-t)^+(b-1))%:E. +Definition beta : set _ -> \bar R := + @mscale _ _ R (invr_nonneg (NngNum B_ge0)) prebeta. + (* \int[lebesgue_measure]_(t in U) @mscale _ _ R (t^+(a-1) (* * (NngNum (onem_ge0 p1))%:num^+(b-1) *) @@ -651,14 +678,24 @@ Definition beta (U : set _) : \bar R := (invr_nonneg (NngNum beta_ge0))%:num)%:nng (mrestr lebesgue_measure (measurable_itv `[0, 1])) U. *) +HB.instance Definition _ := Measure.on beta. + +Let beta_setT : beta [set: _] = 1%:E. +Proof. +rewrite /beta /mscale/= /mrestr/=. +Admitted. + +HB.instance Definition _ := @Measure_isProbability.Build _ _ R + beta beta_setT. + Example __ : beta `[0, 1] = 1%:E. Proof. -rewrite /beta integral_mkcond. +(* rewrite /beta integral_mkcond. rewrite integral_uniform//=. -rewrite oppr0 addr0 invr1 mul1e. +rewrite oppr0 addr0 invr1 mul1e. *) Admitted. -HB.instance Definition _ (p : {nonneg R}) (p1 : p%:num <= 1) +(* HB.instance Definition _ (p : {nonneg R}) (p1 : p%:num <= 1) := Measure.on (beta p1). Let beta_setT (p : {nonneg R}) (p1 : p%:num <= 1) @@ -671,7 +708,7 @@ rewrite ltr01 oppr0 adde0 mule1 /B /Beta. Admitted. HB.instance Definition _ (p : {nonneg R}) (p1 : p%:num <= 1) := @Measure_isProbability.Build _ _ R - (beta p1) (beta_setT p1). + (beta p1) (beta_setT p1). *) (* Lemma __ : beta_probability 6 4 (p1S 2) `[0, 1] = 1%:E. Proof. From b36d7f97159cace0fde35914efc9147a55e66b4a Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Tue, 20 Feb 2024 12:42:59 +0900 Subject: [PATCH 20/36] WIP - clean up def of beta - beta_nat_bern_bern --- _CoqProject | 1 + theories/Make | 1 + theories/lang_syntax.v | 196 +++++++++++++++++++++++++-------------- theories/prob_lang.v | 202 +++++++++++++++++++++++------------------ 4 files changed, 242 insertions(+), 158 deletions(-) diff --git a/_CoqProject b/_CoqProject index bfb8be0be..419dce077 100644 --- a/_CoqProject +++ b/_CoqProject @@ -99,3 +99,4 @@ theories/lang_syntax_util.v theories/lang_syntax_toy.v theories/lang_syntax.v theories/lang_syntax_examples.v +theories/lang_syntax_examples_wip.v diff --git a/theories/Make b/theories/Make index bfe010956..e3e65bc74 100644 --- a/theories/Make +++ b/theories/Make @@ -65,3 +65,4 @@ lang_syntax_util.v lang_syntax_toy.v lang_syntax.v lang_syntax_examples.v +lang_syntax_examples_wip.v diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index 40a8483a1..c855891df 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -277,7 +277,7 @@ Fixpoint measurable_of_typ (t : typ) : {d & measurableType d} := | Unit => existT _ _ munit | Bool => existT _ _ mbool | Nat => existT _ _ (nat : measurableType _) - | Real => existT _ _ + | Real => existT _ _ [the measurableType _ of (@measurableTypeR R)] (* (Real_sort__canonical__measure_Measurable R) *) | Pair A B => existT _ _ @@ -346,74 +346,132 @@ End accessor_functions. Arguments acc_typ {R} s n. Arguments measurable_acc_typ {R} s n. - -Section beta. +(* part of the step from casino3 to casino4 *) +Section casino3_casino4. Context {R : realType}. -(* Check sample_cst (beta 6 4) : R.-sfker _ ~> R. *) -(* Check sample_cst (beta 6 4) : R.-ker _ ~> measurableTypeR R. *) -Check sample_cst (uniform_probability _) : R.-ker _ ~> measurableTypeR R. +Variables a b a' b' : nat. + +Local Notation mu := lebesgue_measure. Open Scope ring_scope. -Lemma a01 : 0 < 1 - 0 :> R. Proof. by []. Qed. Import Notations. -(* Lemma beta11_uniform : - beta 1 1 `[0, 1] = uniform_probability a01 `[0, 1]. +Program Definition beta_nat_bern : R.-sfker munit ~> mbool := + @letin' _ _ _ munit _ mbool _ + (sample_cst [the probability _ _ of (beta_nat a b)]) + (sample (bernoulli_trunc \o ubeta_nat_pdf a' b' \o @fst (measurableTypeR R) _ ) _). +Next Obligation. +apply: measurableT_comp. + apply: measurableT_comp. + exact: measurable_bernoulli_trunc. + exact: measurable_ubeta_nat_pdf. +exact: (measurable_acc_typ [:: Real] 0). +Qed. + +Local Notation B := beta_nat_norm. + +Definition Baa'bb'Bab : R := (B (a + a') (b + b')) / B a b. + +Lemma Baa'bb'Bab_ge0 : 0 <= Baa'bb'Bab. +Proof. by rewrite /Baa'bb'Bab divr_ge0// beta_nat_norm_ge0. Qed. + +Definition Baa'bb'Bab_nneg : {nonneg R} := NngNum Baa'bb'Bab_ge0. + +Lemma Baa'bb'Bab_le1 : Baa'bb'Bab_nneg%:num <= 1. Proof. -rewrite /beta /uniform_probability. -congr mscale. -congr invr_nonneg. -admit. -rewrite /mscale. -apply: funext=> x. *) - -Definition beta_bern : R.-sfker munit ~> mbool := - @letin' _ _ _ munit _ mbool R - (sample_cst (beta 6 4)) - (* (sample_cst (uniform_probability a01)) *) - (sample (bernoulli_trunc \o (@fst (measurableTypeR R) _)) (measurableT_comp measurable_bernoulli_trunc (measurable_acc_typ [:: Real] 0))). - -Lemma letin'_sample_uniform d d' (T : measurableType d) - (T' : measurableType d') (a b : R) (ab0 : (0 < b - a)%R) - (u : R.-sfker [the measurableType _ of (_ * T)%type] ~> T') x y : - measurable y -> - letin' (sample_cst (uniform_probability ab0)) u x y = - ((b - a)^-1%:E * \int[lebesgue_measure]_(x0 in `[a, b]) u (x0, x) y)%E. +rewrite /Baa'bb'Bab_nneg/= /Baa'bb'Bab. +rewrite ler_pdivrMr// ?mul1r ?beta_nat_norm_gt0//. +rewrite /B /beta_nat_norm. +rewrite ler_pdivrMr ?ltr0n ?fact_gt0//. +rewrite mulrAC. +rewrite ler_pdivlMr ?ltr0n ?fact_gt0//. +(* TODO: plausible *) Admitted. -Definition uni_bern : R.-sfker munit ~> mbool := - @letin' _ _ _ munit (measurableTypeR R) mbool R - (sample_cst (uniform_probability a01)) - (* (sample_cst (uniform_probability a01)) *) - (sample (bernoulli_trunc \o (@fst (measurableTypeR R) _)) (measurableT_comp measurable_bernoulli_trunc (measurable_acc_typ [:: Real] 0))). +Lemma onem_Baa'bb'Bab_ge0 : 0 <= B a b - (Baa'bb'Bab_nneg%:num). +Admitted. + +Lemma ubeta_nat_pdf_ge0' t : 0 <= ubeta_nat_pdf a' b' t :> R. +Admitted. + +Lemma ubeta_nat_pdf_le1' t : (NngNum (ubeta_nat_pdf_ge0' t))%:num <= 1 :> R. +Admitted. + +Lemma integral_ubeta_nat : + (\int[ubeta_nat a b]_x (ubeta_nat_pdf a' b' x)%:E = + \int[mu]_(x in `[0%R, 1%R]) + (x ^+ a'.-1 * `1-x ^+ b'.-1 * x ^+ a * `1-x ^+ b)%:E :> \bar R)%E. +Admitted. -Lemma ex_beta_bern : beta_bern tt [set true] = (6 / 10)%:E. +Lemma beta_nat_bern_bern U : + beta_nat_bern tt U = + sample_cst (bernoulli Baa'bb'Bab_le1) tt U. Proof. -rewrite /beta_bern /uni_bern. -rewrite [LHS]letin'E. -rewrite /beta. -rewrite ge0_integral_mscale//. -rewrite /mscale/=/B. -rewrite /prebeta/=. -Search integral lebesgue_measure. - -(* rewrite ge0_integral_mscale//=. -rewrite EFinM. -congr (_ * _)%E. -rewrite /prebeta/=. -Search "pow". -rewrite subn1/=. -transitivity (\int[(integral (uniform_probability p01))^~ (@cst R _ 1%:E)]_x bernoulli_trunc x U)%E. +rewrite /beta_nat_bern. +rewrite [LHS]letin'E/=. +transitivity ((\int[beta_nat a b]_y + (@bernoulli R (NngNum (ubeta_nat_pdf_ge0' y)) (ubeta_nat_pdf_le1' y) U))%E). + apply: eq_integral => /= r _. + rewrite /bernoulli_trunc/=. + case: sumbool_ler; last admit. + move=> H_ge0. + case: sumbool_ler; last admit. + move=> H_le1. + rewrite /=. admit. -rewrite /bernoulli_trunc/=. -rewrite integral_bernoulli_trunc. -(* rewrite /B invr1 !mulr1 fact0 invr1 mul1e. *) -rewrite /prebeta/=. -under eq_integral. -Search integral lebesgue_measure. *) -Abort. +rewrite /beta_nat/=. +transitivity ((((B a b)^-1)%:E * \int[ubeta_nat a b]_x bernoulli (ubeta_nat_pdf_le1' x) U)%E). + apply: ge0_integral_mscale => //=. + admit. +rewrite {2}/bernoulli. +suff: (\int[ubeta_nat a b]_x bernoulli (ubeta_nat_pdf_le1' x) U)%E = + measure_add + (mscale (NngNum (beta_nat_norm_ge0 (a + a') (b + b'))) \d_true) + (mscale (NngNum (onem_Baa'bb'Bab_ge0)) \d_false) U. + admit. +transitivity (( + \int[ubeta_nat a b]_x (mscale (NngNum (ubeta_nat_pdf_ge0' x)) \d_true U) + + + \int[ubeta_nat a b]_x (mscale (onem_nonneg (ubeta_nat_pdf_le1' x)) \d_false) U))%E. + admit. +rewrite measure_addE. +congr adde. + rewrite [in LHS]/mscale/= [in RHS]/mscale/=. + suff: (\int[ubeta_nat a b]_x ((ubeta_nat_pdf a' b' x)%:E) + = (B (a + a') (b + b'))%:E :> \bar R)%E. + admit. + transitivity ( (* calculating distribution (13) *) + \int[mu]_(x in `[(0:R)%R, (1:R)%R]%classic) + ((x ^+ a'.-1 * (`1- x) ^+ b'.-1 * x ^+ a * (`1- x) ^+ b)%:E) + )%E. + by rewrite integral_ubeta_nat. + transitivity ( + (\int[mu]_(x in `[0%R, 1%R]) + ((x ^+ (a + a').-1 * `1-x ^+ (b + b').-1)%:E))%E : \bar R). + admit. + rewrite beta_nat_normE//. + rewrite /ubeta_nat_pdf//. + admit. +rewrite [in LHS]/mscale/= [in RHS]/mscale/=. +suff: (\int[ubeta_nat a b]_x (`1-(ubeta_nat_pdf a' b' x))%:E = + (B a b - B (a + a') (b + b'))%:E :> \bar R)%E. + admit. +under eq_integral do rewrite EFinB/=. +rewrite integralB_EFin//=; last 2 first. + admit. + admit. +rewrite integral_cst//= mul1e. +rewrite beta_nat_normE. +rewrite {1}/ubeta_nat setTI. +rewrite EFinB. +congr (_ - _)%E. + admit. +rewrite integral_ubeta_nat//. +rewrite beta_nat_normE /ubeta_nat_pdf. +admit. +Admitted. -End beta. +End casino3_casino4. Section context. Variables (R : realType). @@ -828,7 +886,7 @@ Inductive evalD : forall g t, exp D g t -> measurable_cst _ | eval_beta g (a b : nat) : - (exp_beta a b : exp D g _) -D> cst (beta a b) ; measurable_cst _ + (exp_beta a b : exp D g _) -D> (cst (beta_nat a b)) ; measurable_cst _ | eval_poisson g n (e : exp D g _) f mf : e -D> f ; mf -> @@ -965,7 +1023,7 @@ all: (rewrite {g t e u v mu mv hu}). by rewrite (IH _ _ H2). - move=> g n p p1 {}v {}mv. inversion 1; subst g0 n0 p0. - inj_ex H2; subst v. + inj_ex H4; subst v. by have -> : p1 = p3 by []. - move=> g n e f mf ev IH {}v {}mv. inversion 1; subst g0 n0. @@ -975,11 +1033,11 @@ all: (rewrite {g t e u v mu mv hu}). by rewrite (IH _ _ H3). - move=> g a b ab0 {}v {}mv. inversion 1; subst g0 a0 b0. - inj_ex H2; subst v. + inj_ex H4; subst v. by have -> : ab0 = ab2. -- move=> g a b {}v {}mv. - inversion 1. subst g0 a0 b0. - by inj_ex H2; subst v. (* TODO: beta *) +- (* TODO: beta *) move=> g a b {}v {}mv. + inversion 1; subst g0 a0 b0. + by inj_ex H4; subst v. - move=> g t e k mk ev IH {}v {}mv. inversion 1; subst g0 t. inj_ex H2; subst e0. @@ -1123,7 +1181,7 @@ all: rewrite {g t e u v eu}. by rewrite (IH _ _ H2). - move=> g n p p1 {}v {}mv. inversion 1; subst g0 n0 p0. - inj_ex H2; subst v. + inj_ex H4; subst v. by have -> : p1 = p3 by []. - move=> g n e f mf ev IH {}v {}mv. inversion 1; subst g0 n0. @@ -1133,11 +1191,11 @@ all: rewrite {g t e u v eu}. by rewrite (IH _ _ H3). - move=> g a b ab0 {}v {}mv. inversion 1; subst g0 a0 b0. - inj_ex H2; subst v. + inj_ex H4; subst v. by have -> : ab0 = ab2. -- move=> g a b {}v {}mv. +- (* TODO: beta case*) move=> g a b {}v {}mv. inversion 1; subst g0 a0 b0. - by inj_ex H2; subst v. + by inj_ex H4; subst v. - move=> g n e f mf ev IH {}v {}mv. inversion 1; subst g0 n0. inj_ex H2; subst e0. @@ -1435,9 +1493,9 @@ Lemma execD_uniform g a b ab0 : existT _ (cst [the probability _ _ of uniform_probability ab0]) (measurable_cst _). Proof. exact/execD_evalD/eval_uniform. Qed. -Lemma execD_beta g a b : +Lemma execD_beta_nat g a b : @execD g _ (exp_beta a b) = - existT _ (cst [the probability _ _ of beta a b]) (measurable_cst _). + existT _ (cst [the probability _ _ of beta_nat a b]) (measurable_cst _). Proof. exact/execD_evalD/eval_beta. Qed. Lemma execD_normalize_pt g t (e : exp P g t) : diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 4fa094059..a1effae86 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -45,7 +45,7 @@ Local Open Scope ereal_scope. (* Definition mR (R : realType) : Type := R. HB.instance Definition _ (R : realType) := Measurable.on (mR R). -(* [the measurableType (R.-ocitv.-measurable).-sigma of +(* [the measurableType (R.-ocitv.-measurable).-sigma of salgebraType (R.-ocitv.-measurable)]. *) *) Module Notations. @@ -57,7 +57,7 @@ Notation var2of3 := (measurableT_comp (@measurable_snd _ _ _ _) (@measurable_fst _ _ _ _)). Notation var3of3 := (@measurable_snd _ _ _ _).*) -(* Definition mR R := [the measurableType (R.-ocitv.-measurable).-sigma of +(* Definition mR R := [the measurableType (R.-ocitv.-measurable).-sigma of salgebraType (R.-ocitv.-measurable)]. *) Notation munit := Datatypes_unit__canonical__measure_Measurable. Notation mbool := Datatypes_bool__canonical__measure_Measurable. @@ -534,7 +534,7 @@ move=> mE m. by rewrite !integral_indic//= /uniform_probability/= /mscale/= /mrestr setIT. Qed. -Let integral_uniform_nnsfun (f : {nnsfun measurableTypeR R >-> R}) +Let integral_uniform_nnsfun (f : {nnsfun _ >-> R}) (a b : R) (ab0 : (0 < b - a)%R) : let m := uniform_probability ab0 in \int[m]_x (f x)%:E = (b - a)^-1%:E * \int[lebesgue_measure]_(x in `[a, b]) (f x)%:E. @@ -563,7 +563,7 @@ apply: eq_fsbigr => r _; rewrite ge0_integralZl//. - by rewrite lee_fin invr_ge0// ltW. Qed. -Lemma integral_uniform (f : measurableTypeR R -> \bar R) (a b : R) +Lemma integral_uniform (f : _ -> \bar R) (a b : R) (ab0 : (0 < b - a)%R) : measurable_fun setT f -> (forall x, 0 <= f x) -> let m := uniform_probability ab0 in \int[m]_x f x = @@ -598,131 +598,155 @@ Qed. End integral_uniform. +(* definition of the beta probability specialized to natural numbers *) Section beta_probability. -Context {R : realType}. Local Open Scope ring_scope. -Variables (a b : nat). +Context {R : realType}. +Variables a b : nat. -Definition Beta t : R := - t^+(a-1) * (1-t)^+(b-1). +(* unnormalized pdf for beta specialized to nat *) +Definition ubeta_nat_pdf (t : R) := t ^+ a.-1 * (`1- t) ^+ b.-1. -Definition B : R := - (a-1)`!%:R * (b-1)`!%:R / (a+b-1)`!%:R. - (* \int[lebesgue_measure]_(t in `[0%R, 1%R]) Beta t. *) +Lemma ubeta_nat_pdf_ge0 t : 0 <= t <= 1 -> 0 <= ubeta_nat_pdf t. +Proof. by move=> /andP[t0 t1]; rewrite mulr_ge0// exprn_ge0// onem_ge0. Qed. -Axiom beta1 : \int[lebesgue_measure]_(x in `[0, 1]) (x^+(a-1) * (1-x)^+(b-1)) = B. +Lemma ubeta_nat_pdf_le1 t : 0 <= t <= 1 -> ubeta_nat_pdf t <= 1. +Proof. +move=> /andP[t0 t1]; rewrite /ubeta_nat_pdf. +by rewrite mulr_ile1// ?(exprn_ge0,onem_ge0,exprn_ile1,onem_le1). +Qed. -(* apply: fine_ge0. -apply: integral_ge0 => x x01. -have /andP[x0 x1] : (0 <= x <= 1) by apply: x01. -rewrite lee_fin. -apply: mulr_ge0; apply/exprn_ge0; lra. *) +Lemma measurable_ubeta_nat_pdf : measurable_fun setT ubeta_nat_pdf. +Proof. +by apply /measurable_funM => //; exact/measurable_fun_pow/measurable_funB. +Qed. -Set Printing All. +(* normalization constant *) +Definition beta_nat_norm : R := a.-1`!%:R * b.-1`!%:R / (a + b).-1`!%:R. -Lemma B_ge0 : 0 <= B. +Lemma beta_nat_normE : + beta_nat_norm = \int[lebesgue_measure]_(x in `[0, 1]) ubeta_nat_pdf x. Proof. -rewrite /B. Admitted. -(* Definition betaPDF (p : R) := - if 0 <= p <= 1 then Beta p / B else 0. +Lemma beta_nat_norm_gt0 : 0 < beta_nat_norm. +Proof. +by rewrite /beta_nat_norm divr_gt0// ?mulr_gt0 ?ltr0n ?fact_gt0. +Qed. + +Lemma beta_nat_norm_ge0 : 0 <= beta_nat_norm. +Proof. exact/ltW/beta_nat_norm_gt0. Qed. -Lemma measurable_beta : measurable_fun setT betaPDF. +(* normalized pdf for beta specialized to nat *) +Definition beta_nat_pdf t := ubeta_nat_pdf t / beta_nat_norm. + +Lemma measurable_beta_nat_pdf : measurable_fun setT beta_nat_pdf. +Proof. by apply: measurable_funM => //; exact: measurable_ubeta_nat_pdf. Qed. + +Lemma beta_nat_pdf_ge0 t : 0 <= t <= 1 -> 0 <= beta_nat_pdf t. Proof. -rewrite /betaPDF; apply: measurable_fun_if => //. - apply: (measurable_fun_bool true). - by rewrite (_ : _ @^-1` _ = `[0, 1]%classic). -apply: measurable_funM=> //; apply /measurable_funM => //. -apply/measurable_fun_pow/measurable_funB => //. -Qed. *) +move=> t01; rewrite /beta_nat_pdf divr_ge0//. + exact: ubeta_nat_pdf_ge0. +exact: beta_nat_norm_ge0. +Qed. -(* TODO: beta_ge0 a b : is_true (0 <= beta a b) (to remove %E) *) -Definition p01 : 0 < 1 - 0 :> R. -Proof. by []. Qed. +Local Notation mu := lebesgue_measure. -Definition prebeta U : \bar R := - (* \int[lebesgue_measure]_(x in U `&` `[0, 1]) (x^+(a-1) * (1-x)^+(b-1))%:E. *) - \int[uniform_probability p01]_(t in U) (t^+(a-1) * (1-t)^+(b-1))%:E. +(* unnormalized beta specialized to nat *) +Definition ubeta_nat (U : set (measurableTypeR R)) : \bar R := + \int[mu]_(x in U `&` `[0, 1](*NB: is this correct?*)) (ubeta_nat_pdf x)%:E. +(* TODO: define as \int[uniform_probability p01]_(t in U) (ubeta_nat_pdf t)%:E ? *) -Let prebeta0 : prebeta set0 = 0%:E. +Let ubeta_nat0 : ubeta_nat set0 = 0%:E. +Proof. by rewrite /ubeta_nat set0I integral_set0. Qed. + +Let ubeta_nat_ge0 U : (0 <= ubeta_nat U)%E. Proof. -(* by rewrite /prebeta integral_setI_indic// integral_set0. *) -by rewrite /prebeta integral_set0. +rewrite /ubeta_nat integral_ge0//= => x [Ux]. +by rewrite in_itv/= => x01; rewrite lee_fin ubeta_nat_pdf_ge0. Qed. -Let prebeta_ge0 U : (0 <= prebeta U)%E. +(* TODO: doable *) +Let ubeta_nat_sigma_additive : semi_sigma_additive ubeta_nat. Proof. -rewrite /prebeta. -rewrite integral_ge0// => x Ux. +move=> /= F mF tF mUF. +rewrite /ubeta_nat. +rewrite setI_bigcupl. +apply: cvg_toP. + apply: ereal_nondecreasing_is_cvgn => m n mn. + admit. +rewrite ge0_integral_bigcup//=. +admit. +admit. +admit. +admit. Admitted. -Let prebeta_sigma_additive : semi_sigma_additive prebeta. -Proof. move=> /= F mF tF mUF. -rewrite /prebeta. -Admitted. +HB.instance Definition _ := isMeasure.Build _ _ _ ubeta_nat + ubeta_nat0 ubeta_nat_ge0 ubeta_nat_sigma_additive. -HB.instance Definition _ := isMeasure.Build _ _ _ prebeta - prebeta0 prebeta_ge0 prebeta_sigma_additive. +Definition beta_nat (*: set [the measurableType (R.-ocitv.-measurable).-sigma of + salgebraType R.-ocitv.-measurable] -> \bar R*) := + @mscale _ _ _ (invr_nonneg (NngNum beta_nat_norm_ge0)) ubeta_nat. -(* HB.instance Definition _ := Measure.on prebeta. *) +HB.instance Definition _ := Measure.on beta_nat. -(* 1/B(a, b) * \int_U p^(a-1) * (1-p)^(b-1) dx = beta *) -Definition beta : set _ -> \bar R := - @mscale _ _ R (invr_nonneg (NngNum B_ge0)) prebeta. +(*Let beta_nat0 : beta_nat set0 = 0. +Proof. exact: measure0. Qed. - (* \int[lebesgue_measure]_(t in U) - @mscale _ _ R (t^+(a-1) - (* * (NngNum (onem_ge0 p1))%:num^+(b-1) *) - * - (invr_nonneg (NngNum beta_ge0))%:num)%:nng - (mrestr lebesgue_measure (measurable_itv `[0, 1])) U. *) +Let beta_nat_ge0 U : (0 <= beta_nat U)%E. +Proof. exact: measure_ge0. Qed. -HB.instance Definition _ := Measure.on beta. +Let beta_nat_sigma_additive : semi_sigma_additive beta_nat. +Proof. move=> /= F mF tF mUF; exact: measure_semi_sigma_additive. Qed. + +HB.instance Definition _ := isMeasure.Build _ _ _ beta_nat beta_nat0 beta_nat_ge0 beta_nat_sigma_additive.*) -Let beta_setT : beta [set: _] = 1%:E. +Let beta_nat_setT : beta_nat setT = 1%:E. Proof. -rewrite /beta /mscale/= /mrestr/=. +rewrite /beta_nat. +rewrite /=. +rewrite /mscale /=. +rewrite beta_nat_normE /ubeta_nat setTI. +(* TODO: doable *) Admitted. -HB.instance Definition _ := @Measure_isProbability.Build _ _ R - beta beta_setT. +HB.instance Definition _ := @Measure_isProbability.Build _ _ _ -Example __ : beta `[0, 1] = 1%:E. + beta_nat beta_nat_setT. + +Lemma beta_nat01 : beta_nat `[0, 1] = 1%:E. Proof. -(* rewrite /beta integral_mkcond. -rewrite integral_uniform//=. -rewrite oppr0 addr0 invr1 mul1e. *) +rewrite /beta_nat. +rewrite /mscale/= /beta_nat /ubeta_nat. +rewrite beta_nat_normE setIid. Admitted. -(* HB.instance Definition _ (p : {nonneg R}) (p1 : p%:num <= 1) - := Measure.on (beta p1). +End beta_probability. -Let beta_setT (p : {nonneg R}) (p1 : p%:num <= 1) - : beta p1 [set: _] = 1%:E. -Proof. -rewrite /beta /mscale/= /mrestr/=. -rewrite setTI lebesgue_measure_itv/= lte_fin. -rewrite ltr01 oppr0 adde0 mule1 /B /Beta. -(* rewrite -subr_gt0 -EFinD . -EFinM mulVf// gt_eqF// subr_gt0. *) -Admitted. +Section beta_probability11. +Local Open Scope ring_scope. +Context {R : realType}. -HB.instance Definition _ (p : {nonneg R}) (p1 : p%:num <= 1) := @Measure_isProbability.Build _ _ R - (beta p1) (beta_setT p1). *) +Lemma ubeta_nat_pdf11 : ubeta_nat_pdf 1 1 = @cst R _ 1. +Proof. by apply/funext => r; rewrite /ubeta_nat_pdf/= !expr0 mulr1. Qed. -(* Lemma __ : beta_probability 6 4 (p1S 2) `[0, 1] = 1%:E. -Proof. -rewrite /beta_probability/beta/= /mscale/=. -rewrite lebesgue_measure_itv. *) +Lemma beta_nat_norm11 : beta_nat_norm 1 1 = 1 :> R. +Proof. by rewrite /beta_nat_norm/= fact0 mulr1/= divff. Qed. -Let H01 : (0 < 1 - 0 :> R). -Proof. lra. Qed. +Let a01 : 0 < 1 - 0 :> R. Proof. by []. Qed. -(* Lemma uniform_beta U : (\int[uniform_probability H01]_x - \int[mscale (NngNum (normr_ge0 (56 * x ^+ 5 * (1 - x) ^+ 3)%R)) (measure_dirac__canonical__measure_Measure tt R)]__ - bernoulli_trunc (1 - (1 - x) ^+ 3) U)%E = 1%:E. *) - -End beta_probability. +Lemma beta11_uniform U : measurable U -> + beta_nat 1 1 U = uniform_probability a01 U. +Proof. +move=> mU; rewrite /beta_nat /uniform_probability. +rewrite /mscale/= beta_nat_norm11 subr0 invr1 !mul1e. +rewrite /ubeta_nat /mrestr/=. +rewrite ubeta_nat_pdf11/= integral_cst/= ?mul1e//. +exact: measurableI. +Qed. + +End beta_probability11. Section mscore. Context d (T : measurableType d) (R : realType). From d2f1140e7f42d312f7600b00f8968acf6671679d Mon Sep 17 00:00:00 2001 From: AyumuSaito Date: Tue, 20 Feb 2024 17:31:59 +0900 Subject: [PATCH 21/36] progress in proving the casino exampl - rewrite casino01 - proved casino34' - casino34 (remain admit) - casino45 - add Normalize --- theories/lang_syntax.v | 15 +- theories/lang_syntax_examples_wip.v | 345 ++++++++++++++++------------ theories/prob_lang.v | 24 +- 3 files changed, 231 insertions(+), 153 deletions(-) diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index c855891df..a231d1ac9 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -370,7 +370,7 @@ Qed. Local Notation B := beta_nat_norm. -Definition Baa'bb'Bab : R := (B (a + a') (b + b')) / B a b. +Definition Baa'bb'Bab : R := (beta_nat_norm (a + a') (b + b')) / beta_nat_norm a b. Lemma Baa'bb'Bab_ge0 : 0 <= Baa'bb'Bab. Proof. by rewrite /Baa'bb'Bab divr_ge0// beta_nat_norm_ge0. Qed. @@ -417,8 +417,8 @@ transitivity ((\int[beta_nat a b]_y move=> H_ge0. case: sumbool_ler; last admit. move=> H_le1. - rewrite /=. - admit. + rewrite /=/bernoulli !measure_addE/= /mscale/=. + congr _%E. rewrite /beta_nat/=. transitivity ((((B a b)^-1)%:E * \int[ubeta_nat a b]_x bernoulli (ubeta_nat_pdf_le1' x) U)%E). apply: ge0_integral_mscale => //=. @@ -454,7 +454,7 @@ congr adde. admit. rewrite [in LHS]/mscale/= [in RHS]/mscale/=. suff: (\int[ubeta_nat a b]_x (`1-(ubeta_nat_pdf a' b' x))%:E = - (B a b - B (a + a') (b + b'))%:E :> \bar R)%E. + (B a b - B (a + a'.-1) (b + b'.-1))%:E :> \bar R)%E. admit. under eq_integral do rewrite EFinB/=. rewrite integralB_EFin//=; last 2 first. @@ -465,9 +465,14 @@ rewrite beta_nat_normE. rewrite {1}/ubeta_nat setTI. rewrite EFinB. congr (_ - _)%E. + (* rewrite fineK//. *) admit. rewrite integral_ubeta_nat//. rewrite beta_nat_normE /ubeta_nat_pdf. +under eq_integral => x _. + rewrite -mulrA -mulrCA !mulrA/= -exprD -mulrA [X in _ * X]mulrC -exprD. +over. +rewrite /=. admit. Admitted. @@ -886,7 +891,7 @@ Inductive evalD : forall g t, exp D g t -> measurable_cst _ | eval_beta g (a b : nat) : - (exp_beta a b : exp D g _) -D> (cst (beta_nat a b)) ; measurable_cst _ + (exp_beta a b : exp D g _) -D> cst (beta_nat a b) ; measurable_cst _ | eval_poisson g n (e : exp D g _) f mf : e -D> f ; mf -> diff --git a/theories/lang_syntax_examples_wip.v b/theories/lang_syntax_examples_wip.v index d96074b1c..2452c1ea2 100644 --- a/theories/lang_syntax_examples_wip.v +++ b/theories/lang_syntax_examples_wip.v @@ -55,9 +55,33 @@ case: (sumbool_ler 0 p%:num) => [{}p0/=|]. admit. Abort. - End trunc_lemmas. +Section beta_example. +Open Scope ring_scope. +Open Scope lang_scope. +Context (R : realType). + +Let p610 : ((6 / 10%:R)%:nng : {nonneg R})%:num <= 1. +Proof. admit. Admitted. + +Lemma beta_bernoulli : + @execP R [::] _ [let "p" := Sample {exp_beta 6 4} in Sample {exp_bernoulli_trunc [#{"p"}]}] = + execP [Sample {@exp_bernoulli _ _ (6 / 10%:R)%:nng p610}]. +Proof. +rewrite execP_letin !execP_sample !execD_beta_nat !execD_bernoulli/=. +rewrite execD_bernoulli_trunc exp_var'E !(execD_var_erefl "p")/=. +apply: eq_sfkernel=> x U. +rewrite letin'E/=. +rewrite /beta_nat/mscale/=. +transitivity (bernoulli_trunc ((@beta_nat_norm R 7 4) / (@beta_nat_norm R 6 4)) U); last first. + (* congr (bernoulli_trunc _ _). *) + (* rewrite /beta_nat_norm/= factE/=; lra. *) +(* apply: beta_nat_bern_bern. *) +Admitted. + +End beta_example. + Section casino_example. Open Scope ring_scope. Open Scope lang_scope. @@ -81,42 +105,7 @@ rewrite !letin'E !iteE/= integral_dirac// ge0_integral_mscale//=. by rewrite normr0 mul0e !mule0 !adde0 !diracT !mul1e. Qed. -(* Definition ex : exp _ [::] _ := @exp_bernoulli R [::] (1 / 2)%:nng (p1S 1). -Example ex1 : projT1 (execD ex) tt = 1%:E. *) - -Definition casino0 : exp _ [::] Bool := - [let "p" := Sample {exp_uniform 0 1 a01} in - let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in - let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in - let "a2" := Sample {exp_binomial_trunc 3 [#{"p"}]} in - return {1}:N <= #{"a2"}]. - -Example e1 : @exp R _ [::] _ := [{1}:R + {2}:R * {2}:R ^+ {3%nat}]. - -Lemma exe1 : projT1 (execD e1) = (fun x => 17). -Proof. -rewrite /e1 (@execD_bin _ _ binop_add) (@execD_bin _ _ binop_mult)/=. -rewrite execD_pow/= !execD_real /=. -apply: funext => x /=. -lra. -Qed. - -(* Arguments exp_bin {R g b} &. *) -Definition casino1 : @exp R _ [::] _ := - [let "p" := Sample {exp_uniform 0 1 a01} in - let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in - let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in - let "a2" := - Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]} in - return #{"a2"}]. - -Definition casino1' : @exp R _ [::] _ := - [let "p" := Sample {exp_uniform 0 1 a01} in - let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in - let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in - Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. - -Lemma binomial_le1' n p U : +Lemma binomial_over1 n p U : 0 <= p <= 1 -> (\int[binomial_probability_trunc n p]_y0 \d_(0 < y0)%N U = bernoulli_trunc (1 - `1-p ^+ n) U :> \bar R)%E. @@ -235,9 +224,6 @@ rewrite {}/A {}/B !setTI /ysection/= (*TODO: lemma?*) /preimage/=. by apply/seteqP; split => [z|z] /=; rewrite inE/=. Qed. -Let weak_head fl g {t1 t2} x (e : @exp R fl g t2) (xg : x \notin dom g) := - exp_weak fl [::] _ (x, t1) e xg. - Lemma execP_letin_uniform g t str (s0 s1 : exp P ((str, Real) :: g) t) : (forall (p : R) x U, 0 <= p <= 1 -> execP s0 (p, x) U = execP s1 (p, x) U) -> @@ -254,96 +240,27 @@ apply: s01. by rewrite inE in p01. Qed. -(* Lemma casino01 : execP casino0 = execP casino1. -Proof. -rewrite /casino0 /casino1. -rewrite !execP_letin !execP_sample execD_uniform !execD_binomial_trunc /=. -rewrite execP_if execP_score !execP_return !execD_bernoulli_trunc/=. -rewrite !execD_rel (@execD_bin _ _ binop_minus) !execD_real execD_pow/=. -rewrite !execD_nat execD_unit/=. -rewrite !exp_var'E !(execD_var_erefl "p") !(execD_var_erefl "a1")/=. -rewrite !(execD_var_erefl "a2")/=. -do 3 congr letin'. -apply: eq_sfkernel => x U. -rewrite !letin'E/=. -apply: binomial_le1. - -rewrite /=. -Abort. *) - -(* Lemma casino01 : execP casino0 = execP casino1. -Proof. -rewrite /casino0 /casino1. -pose s0 := - [let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in - let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in - let "a2" := Sample {exp_binomial_trunc 3 [#{"p"}]} in - {exp_return [{1}:N <= #{"a2"}]}]. -pose s1 := - [let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in - let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in - let "a2" := Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%N}]} in - {exp_return [#{"a2"}]}]. -have := (@execP_letin_uniform [::] Bool "p" (s0 R (found "p" Real [::]) _ _) (s1 R (found "p" Real [::]) _ _)). -apply. -move=> p x U r01. -rewrite /s0/s1/=. -rewrite !execP_letin !execP_sample !execD_binomial_trunc /=. -rewrite execP_if execP_score !execP_return !execD_bernoulli_trunc/=. -rewrite !execD_rel (@execD_bin _ _ binop_minus) execD_pow. -rewrite (@execD_bin _ _ binop_minus) !execD_real/=. -rewrite !execD_nat execD_unit/=. -rewrite !exp_var'E !(execD_var_erefl "p") !(execD_var_erefl "a1")/=. -rewrite !(execD_var_erefl "a2")/=. -rewrite !letin'E/=. -move: r01 => /andP[r0 r1]. -rewrite !integral_binomial_probabilty_trunc//=. -apply: eq_bigr => i _. -congr (_ * _)%E. -rewrite !letin'E iteE/=. -congr (\int[_]_y _)%E. -apply: funext => x0. -rewrite !letin'E/=. -by apply/binomial_le1/andP. -Qed. *) - -Lemma casino01' y V : measurable V -> execP casino0 y V = execP casino1' y V. +Lemma congr_letin g t1 t2 str (e : @exp _ _ _ t1) (e1 e2 : @exp _ _ (_ :: g) t2) x U : + (forall y V, execP e1 (y, x) V = execP e2 (y, x) V) -> + @execP R g t2 [let str := e in e1] x U = @execP R g t2 [let str := e in e2] x U. Proof. -move=> mV //. -rewrite /casino0 /casino1. -apply: execP_letin_uniform => //. -move=> p x U r01. -rewrite !execP_letin !execP_sample !execD_binomial_trunc /=. -rewrite execP_if execP_score !execP_return !execD_bernoulli_trunc/=. -rewrite !execD_rel (@execD_bin _ _ binop_minus) execD_pow. -rewrite (@execD_bin _ _ binop_minus) !execD_real/=. -rewrite !execD_nat execD_unit/=. -rewrite !exp_var'E !(execD_var_erefl "p") !(execD_var_erefl "a1")/=. -rewrite !(execD_var_erefl "a2")/=. -rewrite !letin'E/=. -move: r01 => /andP[r0 r1]. -rewrite !integral_binomial_probabilty_trunc//=. -apply: eq_bigr => i _. -congr (_ * _)%E. -rewrite !letin'E iteE/=. -congr (\int[_]_y _)%E. -apply: funext => x0. -rewrite !letin'E/=. -by apply/binomial_le1'/andP. +move=> He. +rewrite !execP_letin !letin'E. +apply: eq_integral => ? _. +apply: He. Qed. -Lemma exec_casino t U : - execP casino0 t U = ((10 / 99)%:E * \d_true U + (1 / 99)%:E * \d_false U)%E . +Lemma congr_normalize g t (e1 e2 : @exp R _ g t) : + (forall x U, execP e1 x U = execP e2 x U) -> + execD [Normalize e1] = execD [Normalize e2]. Proof. -rewrite /casino0 !execP_letin !execP_sample execD_uniform/=. -rewrite !execD_binomial_trunc execP_if !execP_return !execP_score/=. -rewrite !execD_rel !execD_real execD_unit/=. -rewrite !exp_var'E (execD_var_erefl "p") (execD_var_erefl "a1"). -rewrite (execD_var_erefl "p") (execD_var_erefl "a2")/=. -rewrite letin'E/= /uniform_probability ge0_integral_mscale//=. -rewrite subr0 invr1 mul1e. -under eq_integral. -Admitted. +move=> He. +apply: eq_execD. +rewrite !execD_normalize_pt /=. +f_equal. +apply: eq_kernel => y V. +apply: He. +Qed. Definition uniform_syntax : @exp R _ [::] _ := [let "p" := Sample {exp_uniform 0 1 a01} in @@ -414,17 +331,72 @@ congr (_%:E * _)%E. lra. Qed. +Lemma exec_beta_a1 U : + @execP R [::] _ [let "p" := Sample {exp_beta 6 4} in + Sample {exp_bernoulli_trunc [#{"p"}]}] tt U = + @execP R [::] _ [Sample {exp_bernoulli_trunc [{3 / 5}:R]}] tt U. +Proof. +rewrite execP_letin !execP_sample execD_beta_nat !execD_bernoulli_trunc/=. +rewrite !execD_real/= exp_var'E (execD_var_erefl "p")/=. +transitivity (beta_nat_bern 6 4 1 0 tt U : \bar R). + rewrite /beta_nat_bern !letin'E/= /ubeta_nat_pdf/= /onem. + apply: eq_integral => x _. + do 2 f_equal. + by rewrite (mul1r, mulr1). +rewrite beta_nat_bern_bern/= /bernoulli/= bernoulli_truncE; last by lra. +rewrite measure_addE/= /mscale/=. +congr (_ * _ + _ * _)%:E; rewrite /onem; rewrite /Baa'bb'Bab /beta_nat_norm/=; +by rewrite !factS/= fact0; field. +Qed. + +Definition casino0 : @exp R _ [::] _ := + [Normalize + let "p" := Sample {exp_uniform 0 1 a01} in + let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in + let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in + let "a2" := Sample {exp_binomial_trunc 3 [#{"p"}]} in + return {1}:N <= #{"a2"}]. + +Definition casino1 : @exp R _ [::] _ := + [Normalize + let "p" := Sample {exp_uniform 0 1 a01} in + let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in + let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in + Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. + +Lemma casino01 : + execD casino0 = execD casino1. +Proof. +rewrite /casino0 /casino1. +apply: eq_execD. +f_equal. +apply: congr_normalize => y V. +apply: execP_letin_uniform => //. +move=> p x U r01. +apply: congr_letin => y0 V0. +apply: congr_letin => y1 V1. +rewrite !execP_letin !execP_sample !execD_binomial_trunc /=. +rewrite !execP_return !execD_bernoulli_trunc/=. +rewrite !execD_rel (@execD_bin _ _ binop_minus) execD_pow. +rewrite (@execD_bin _ _ binop_minus) !execD_real/= !execD_nat. +rewrite !exp_var'E !(execD_var_erefl "p") !(execD_var_erefl "a2")/=. +rewrite !letin'E/=. +move: r01 => /andP[r0 r1]. +by apply/binomial_over1/andP. +Qed. + Definition casino2 : @exp R _ [::] _ := - [let "p" := Sample {exp_uniform 0 1 a01} in + [Normalize + let "p" := Sample {exp_uniform 0 1 a01} in let "_" := Score {[{56}:R * #{"p"} ^+ {5%nat} * {[{1}:R - #{"p"}]} ^+ {3%nat}]} in Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. -Lemma casino12 y V : - measurable V -> - execP casino1' y V = execP casino2 y V. +Lemma casino12 : + execD casino1 = execD casino2. Proof. -move=> mV. -rewrite /casino1' /casino2. +apply: eq_execD. +f_equal. +apply: congr_normalize => y V. apply: execP_letin_uniform => //. move=> p x U /andP[p0 p1]. rewrite !execP_letin !execP_sample execP_if execD_rel/=. @@ -436,7 +408,6 @@ rewrite !execD_binomial_trunc/=. rewrite !exp_var'E !(execD_var_erefl "p") !(execD_var_erefl "a1")/=. rewrite !letin'E/=. rewrite integral_binomial_probabilty_trunc//=. -(* set s := letin' _ _. *) rewrite (bigD1 (inord 5))//=. rewrite big1; last first. move=> [[|[|[|[|[|[|[|[|[|//]]]]]]]]]]//= Hi Hi5; rewrite letin'E iteE; @@ -455,23 +426,113 @@ by rewrite subr_ge0. Qed. Definition casino3 : @exp R _ [::] _ := - [let "_" := Score {1 / 9}:R in + [Normalize + let "_" := Score {1 / 9}:R in let "p" := Sample {exp_beta 6 4} in Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. +Lemma casino34' U : + @execP R [::] _ [let "p" := Sample {exp_beta 6 4} in + Sample {exp_bernoulli_trunc [{[{1}:R - #{"p"}]} ^+ {3%nat}]}] tt U = + @execP R [::] _ [Sample {exp_bernoulli_trunc [{1 / 11}:R]}] tt U. +Proof. +rewrite execP_letin !execP_sample execD_beta_nat !execD_bernoulli_trunc/=. +rewrite execD_pow/= (@execD_bin _ _ binop_minus) !execD_real/=. +rewrite exp_var'E (execD_var_erefl "p")/=. +transitivity (beta_nat_bern 6 4 0 3 tt U : \bar R). + rewrite /beta_nat_bern !letin'E/= /ubeta_nat_pdf/= /onem. + apply: eq_integral => x _. + do 2 f_equal. + by rewrite mul1r. +rewrite bernoulli_truncE; last by lra. +rewrite beta_nat_bern_bern/= /bernoulli/=. +rewrite measure_addE/= /mscale/=. +by congr (_ * _ + _ * _)%:E; rewrite /onem; +rewrite /Baa'bb'Bab /beta_nat_norm/=; rewrite !factS/= fact0; field. +Qed. + +Lemma bern_onem (f : _ -> R) U p : + (\int[beta_nat 6 4]_y bernoulli_trunc (f y) U = p%:E * \d_true U + `1-p%:E * \d_false U)%E -> + (\int[beta_nat 6 4]_y bernoulli_trunc (1 - f y) U = `1-p%:E * \d_true U + p%:E * \d_false U)%E. +Proof. +under eq_integral => x _. + rewrite bernoulli_truncE. + over. +admit. +rewrite /= /beta_nat/mscale/= /beta_nat_norm/= /ubeta_nat/ubeta_nat_pdf. +Admitted. + Definition casino4 : @exp R _ [::] _ := - [let "_" := Score {1 / 9}:R in + [Normalize + let "_" := Score {1 / 9}:R in Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. +Lemma casino34 : + execD casino3 = execD casino4. +Proof. +apply: eq_execD. +f_equal. +apply: congr_normalize => y V. +apply: congr_letin => x U. +rewrite execP_letin !execP_sample execD_beta_nat !execD_bernoulli_trunc/=. +rewrite (@execD_bin _ _ binop_minus) execD_pow/= (@execD_bin _ _ binop_minus). +rewrite !execD_real/= exp_var'E (execD_var_erefl "p")/=. +transitivity (\int[beta_nat 6 4]_y bernoulli_trunc (1 - (1 - y) ^+ 3) U : \bar R)%E. + by rewrite /beta_nat_bern !letin'E/= /onem. +rewrite bernoulli_truncE; last by lra. +have -> := (@bern_onem (fun x => (1 - x) ^+ 3) U (1 / 11) _). + congr (_ * _ + _ * _)%E; congr _%:E; rewrite /onem; lra. +transitivity (beta_nat_bern 6 4 0 3 tt U : \bar R). + rewrite /beta_nat_bern !letin'E/= /ubeta_nat_pdf/= /onem. + apply: eq_integral => y0 _. + do 2 f_equal. + by rewrite mul1r. +rewrite beta_nat_bern_bern/= /bernoulli/=. +rewrite measure_addE/= /mscale/=. +by congr (_ * _ + _ * _)%:E; rewrite /onem; +rewrite /Baa'bb'Bab /beta_nat_norm/=; rewrite !factS/= fact0; field. +Qed. + +Lemma norm_score_bern g p1 p2 (p10 : p1 != 0) (p1_ge0 : 0 <= p1) +(p201 : 0 <= p2 <= 1) : + @execD R g _ + [Normalize let "_" := Score {p1}:R in + Sample {exp_bernoulli_trunc [{p2}:R]}] = + execD [Normalize Sample {exp_bernoulli_trunc [{p2}:R]}]. +Proof. +apply: eq_execD. +rewrite !execD_normalize_pt/= !execP_letin !execP_score. +rewrite !execP_sample !execD_bernoulli_trunc !execD_real/=. +apply: funext=> x. +apply: eq_probability=> /= y. +rewrite /normalize_pt !normalizeE/=. +rewrite !bernoulli_truncE; last lra; last lra. +rewrite !diracT !mule1 /onem -EFinD addrCA subrr addr0. +rewrite !letin'E. +under eq_integral. + move=> x0 _ /=. + rewrite !bernoulli_truncE; last lra. + rewrite !diracT !mule1 /onem -EFinD addrCA subrr addr0. + over. +rewrite !ge0_integral_mscale//= ger0_norm//. +rewrite integral_dirac// diracT !mule1. +rewrite !ifF; last first. + rewrite eqe. + apply/negbTE/negP => /orP[/eqP|//]. + by rewrite /onem; lra. + rewrite eqe. + apply/negbTE/negP => /orP[/eqP|//]. + by rewrite /onem; lra. +rewrite !bernoulli_truncE; last lra. +rewrite integral_dirac//= diracT !diracE. +by rewrite muleC muleA -EFinM mulVf// invr1 /onem !(mul1r, mule1). +Qed. + Definition casino5 : @exp R _ [::] _ := [Normalize Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. -(* Lemma casino5_ans U : projT1 (execD casino5) tt U = - ((10 / 11)%:E * \d_true U + (1 / 11)%:E * \d_false U)%E. -Proof. -rewrite /casino5 execD_normalize_pt execP_sample execD_bernoulli_trunc/=. -rewrite execD_real/=. -rewrite normalizeE/= bernoulli_truncE. -rewrite /mnormalize/=. *) +Lemma casino45 : + execD casino4 = execD casino5. +Proof. by rewrite norm_score_bern//; lra. Qed. End casino_example. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index a1effae86..9168dd1d8 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -491,7 +491,7 @@ End binomial_example. Section uniform_probability. Context (R : realType) (a b : R) (ab0 : (0 < b - a)%R). -Definition uniform_probability (* : set _ -> \bar R *) := +Definition uniform_probability : set _ -> \bar R := @mscale _ _ R (invr_nonneg (NngNum (ltW ab0))) (mrestr lebesgue_measure (measurable_itv `[a, b])). @@ -605,7 +605,7 @@ Context {R : realType}. Variables a b : nat. (* unnormalized pdf for beta specialized to nat *) -Definition ubeta_nat_pdf (t : R) := t ^+ a.-1 * (`1- t) ^+ b.-1. +Definition ubeta_nat_pdf (t : R) := t ^+ a * (`1- t) ^+ b. Lemma ubeta_nat_pdf_ge0 t : 0 <= t <= 1 -> 0 <= ubeta_nat_pdf t. Proof. by move=> /andP[t0 t1]; rewrite mulr_ge0// exprn_ge0// onem_ge0. Qed. @@ -689,7 +689,19 @@ Definition beta_nat (*: set [the measurableType (R.-ocitv.-measurable).-sigma of salgebraType R.-ocitv.-measurable] -> \bar R*) := @mscale _ _ _ (invr_nonneg (NngNum beta_nat_norm_ge0)) ubeta_nat. -HB.instance Definition _ := Measure.on beta_nat. +Let beta_nat0 : beta_nat set0 = 0. +Proof. exact: measure0. Qed. + +Let beta_nat_ge0 U : (0 <= beta_nat U)%E. +Proof. exact: measure_ge0. Qed. + +Let beta_nat_sigma_additive : semi_sigma_additive beta_nat. +Proof. move=> /= F mF tF mUF; exact: measure_semi_sigma_additive. Qed. + +HB.instance Definition _ := isMeasure.Build _ _ _ beta_nat + beta_nat0 beta_nat_ge0 beta_nat_sigma_additive. + +(* HB.instance Definition _ := Measure.on beta_nat. *) (*Let beta_nat0 : beta_nat set0 = 0. Proof. exact: measure0. Qed. @@ -728,16 +740,16 @@ Section beta_probability11. Local Open Scope ring_scope. Context {R : realType}. -Lemma ubeta_nat_pdf11 : ubeta_nat_pdf 1 1 = @cst R _ 1. +Lemma ubeta_nat_pdf11 : ubeta_nat_pdf 0 0 = @cst R _ 1. Proof. by apply/funext => r; rewrite /ubeta_nat_pdf/= !expr0 mulr1. Qed. -Lemma beta_nat_norm11 : beta_nat_norm 1 1 = 1 :> R. +Lemma beta_nat_norm11 : beta_nat_norm 0 0 = 1 :> R. Proof. by rewrite /beta_nat_norm/= fact0 mulr1/= divff. Qed. Let a01 : 0 < 1 - 0 :> R. Proof. by []. Qed. Lemma beta11_uniform U : measurable U -> - beta_nat 1 1 U = uniform_probability a01 U. + beta_nat 0 0 U = uniform_probability a01 U. Proof. move=> mU; rewrite /beta_nat /uniform_probability. rewrite /mscale/= beta_nat_norm11 subr0 invr1 !mul1e. From b0e08647f03d708aa9bdc2d2b213ed9bd55c8c5c Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Tue, 27 Feb 2024 16:24:15 +0900 Subject: [PATCH 22/36] admit about fact and adjustment of exp but casino broken --- theories/lang_syntax.v | 181 +++++++++++++++++++++++++--- theories/lang_syntax_examples_wip.v | 55 ++++++--- theories/prob_lang.v | 70 ++++++----- 3 files changed, 244 insertions(+), 62 deletions(-) diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index a231d1ac9..6e868ef9d 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -346,6 +346,69 @@ End accessor_functions. Arguments acc_typ {R} s n. Arguments measurable_acc_typ {R} s n. + +Lemma factD n m : (n`! * m`! <= (n + m).+1`!)%N. +Proof. +elim: n m => /= [m|n ih m]. + by rewrite fact0 mul1n add0n factS leq_pmull. +rewrite 2!factS [in X in (_ <= _ * X)%N]addSn -mulnA leq_mul//. +by rewrite ltnS addSnnS leq_addr. +Qed. + +Lemma factD' n m : (n`! * m.-1`! <= (n + m)`!)%N. +Proof. +case: m => //= [|m]. + by rewrite fact0 muln1 addn0. +by rewrite addnS factD. +Qed. + +Lemma leq_prod2 (x y n m : nat) : (n <= x)%N -> (m <= y)%N -> + (\prod_(m <= i < y) i * \prod_(n <= i < x) i <= \prod_(n + m <= i < x + y) i)%N. +Proof. +move=> nx my. +rewrite big_addn. +rewrite -addnBA//. +rewrite {3}/index_iota. +rewrite -addnBAC//. +rewrite iotaD. +rewrite big_cat/=. +rewrite mulnC. +rewrite leq_mul//. + rewrite /index_iota. + apply: leq_prod. + by move=> i _; rewrite leq_addr. +rewrite subnKC//. +rewrite -{1}(add0n m). +rewrite big_addn. +rewrite {2}(_ : (y - m) = ((y - m + x) - x))%N; last first. + by rewrite -addnBA// subnn addn0. +rewrite -{1}(add0n x). +rewrite big_addn. +rewrite -addnBA// subnn addn0. +apply: leq_prod => i _. +by rewrite leq_add2r leq_addr. +Qed. + +Lemma leq_fact2 (x y n m : nat) : + (n <= x) %N -> (m <= y)%N -> + (x`! * y`! * ((n + m).+1)`! <= n`! * m`! * ((x + y).+1)`!)%N. +Proof. +move=> nx my. +rewrite (_ : x`! = n`! * \prod_(n.+1 <= i < x.+1) i)%N; last first. + by rewrite -fact_split. +rewrite -!mulnA leq_mul2l; apply/orP; right. +rewrite (_ : y`! = m`! * \prod_(m.+1 <= i < y.+1) i)%N; last first. + by rewrite -fact_split. +rewrite mulnCA -!mulnA leq_mul2l; apply/orP; right. +rewrite (_ : (x + y).+1`! = (n + m).+1`! * \prod_((n + m).+2 <= i < (x + y).+2) i)%N; last first. + rewrite -fact_split//. + by rewrite ltnS leq_add. +rewrite mulnA mulnC leq_mul2l; apply/orP; right. +rewrite -addSn -addnS. +rewrite -addSn -addnS. +exact: leq_prod2. +Qed. + (* part of the step from casino3 to casino4 *) Section casino3_casino4. Context {R : realType}. @@ -358,7 +421,7 @@ Import Notations. Program Definition beta_nat_bern : R.-sfker munit ~> mbool := @letin' _ _ _ munit _ mbool _ - (sample_cst [the probability _ _ of (beta_nat a b)]) + (sample_cst [the probability _ _ of beta_nat a b]) (sample (bernoulli_trunc \o ubeta_nat_pdf a' b' \o @fst (measurableTypeR R) _ ) _). Next Obligation. apply: measurableT_comp. @@ -375,7 +438,7 @@ Definition Baa'bb'Bab : R := (beta_nat_norm (a + a') (b + b')) / beta_nat_norm a Lemma Baa'bb'Bab_ge0 : 0 <= Baa'bb'Bab. Proof. by rewrite /Baa'bb'Bab divr_ge0// beta_nat_norm_ge0. Qed. -Definition Baa'bb'Bab_nneg : {nonneg R} := NngNum Baa'bb'Bab_ge0. +Definition Baa'bb'Bab_nneg : {nonneg R} := NngNum Baa'bb'Bab_ge0. Lemma Baa'bb'Bab_le1 : Baa'bb'Bab_nneg%:num <= 1. Proof. @@ -385,22 +448,67 @@ rewrite /B /beta_nat_norm. rewrite ler_pdivrMr ?ltr0n ?fact_gt0//. rewrite mulrAC. rewrite ler_pdivlMr ?ltr0n ?fact_gt0//. -(* TODO: plausible *) -Admitted. +rewrite -!natrM ler_nat. +case: a. + rewrite /= fact0 mul1n !add0n. + case: b => /=. + case: a' => //. + case: b' => //= m. + by rewrite fact0 !mul1n muln1. + move=> n/=. + by rewrite fact0 add0n muln1 mul1n factD'. + move=> m. + rewrite mulnC leq_mul// mulnC. + by rewrite (leq_trans (factD' _ _))// addSn addnS//= addnC. +move=> n. +rewrite addSn. +case: b. + rewrite !fact0 add0n muln1 [leqRHS]mulnC addn0/= leq_mul//. + by rewrite factD'. +move=> m. +clear a b. +rewrite [(n + a').+1.-1]/=. +rewrite [n.+1.-1]/=. +rewrite [m.+1.-1]/=. +rewrite addnS. +rewrite [(_ + m).+1.-1]/=. +rewrite (addSn m b'). +rewrite [(m + _).+1.-1]/=. +rewrite (addSn (n + a')). +rewrite [_.+1.-1]/=. +rewrite addSn addnS. +by rewrite leq_fact2// leq_addr. +Qed. -Lemma onem_Baa'bb'Bab_ge0 : 0 <= B a b - (Baa'bb'Bab_nneg%:num). -Admitted. +Lemma onem_Baa'bb'Bab_ge0 : 0 <= 1 - (Baa'bb'Bab_nneg%:num). +Proof. by rewrite subr_ge0 Baa'bb'Bab_le1. Qed. + +Lemma onem_Baa'bb'Bab_ge0_fix : 0 <= B a b * (1 - Baa'bb'Bab_nneg%:num). +Proof. +rewrite mulr_ge0//. + rewrite /B. + exact: beta_nat_norm_ge0. +rewrite subr_ge0. +exact: Baa'bb'Bab_le1. +Qed. Lemma ubeta_nat_pdf_ge0' t : 0 <= ubeta_nat_pdf a' b' t :> R. +Proof. +apply: ubeta_nat_pdf_ge0. (* TODO: needs 0 <= t <= 1 *) Admitted. Lemma ubeta_nat_pdf_le1' t : (NngNum (ubeta_nat_pdf_ge0' t))%:num <= 1 :> R. +Proof. +rewrite /=. +rewrite /ubeta_nat_pdf. +rewrite /ubeta_nat_pdf'. (* TODO: needs 0 <= t <= 1 *) Admitted. Lemma integral_ubeta_nat : (\int[ubeta_nat a b]_x (ubeta_nat_pdf a' b' x)%:E = \int[mu]_(x in `[0%R, 1%R]) (x ^+ a'.-1 * `1-x ^+ b'.-1 * x ^+ a * `1-x ^+ b)%:E :> \bar R)%E. +Proof. Admitted. Lemma beta_nat_bern_bern U : @@ -413,32 +521,65 @@ transitivity ((\int[beta_nat a b]_y (@bernoulli R (NngNum (ubeta_nat_pdf_ge0' y)) (ubeta_nat_pdf_le1' y) U))%E). apply: eq_integral => /= r _. rewrite /bernoulli_trunc/=. - case: sumbool_ler; last admit. + case: sumbool_ler; last first. + admit. move=> H_ge0. - case: sumbool_ler; last admit. + case: sumbool_ler; last first. + admit. move=> H_le1. rewrite /=/bernoulli !measure_addE/= /mscale/=. - congr _%E. + by congr _%E. rewrite /beta_nat/=. -transitivity ((((B a b)^-1)%:E * \int[ubeta_nat a b]_x bernoulli (ubeta_nat_pdf_le1' x) U)%E). - apply: ge0_integral_mscale => //=. +transitivity (((B a b)^-1%:E * \int[ubeta_nat a b]_x bernoulli (ubeta_nat_pdf_le1' x) U)%E). + rewrite ge0_integral_mscale //=. admit. rewrite {2}/bernoulli. suff: (\int[ubeta_nat a b]_x bernoulli (ubeta_nat_pdf_le1' x) U)%E = measure_add (mscale (NngNum (beta_nat_norm_ge0 (a + a') (b + b'))) \d_true) - (mscale (NngNum (onem_Baa'bb'Bab_ge0)) \d_false) U. - admit. -transitivity (( + (mscale (NngNum (onem_Baa'bb'Bab_ge0_fix)) \d_false) U. + rewrite /= => ->. + rewrite !measure_addE/= /mscale/=. + rewrite /Baa'bb'Bab/=. + rewrite muleDr//. + congr (_ + _)%E. + rewrite -EFinM. + rewrite mulrA. + rewrite (mulrC (_^-1)). + by rewrite EFinM. + rewrite muleA. + congr (_ * _)%E. + rewrite -EFinM. + rewrite mulrA mulVr ?mul1r ?unitfE//. + rewrite gt_eqF//. + by rewrite beta_nat_norm_gt0. +transitivity ( \int[ubeta_nat a b]_x (mscale (NngNum (ubeta_nat_pdf_ge0' x)) \d_true U) + - \int[ubeta_nat a b]_x (mscale (onem_nonneg (ubeta_nat_pdf_le1' x)) \d_false) U))%E. - admit. + \int[ubeta_nat a b]_x (mscale (onem_nonneg (ubeta_nat_pdf_le1' x)) \d_false) U)%E. + rewrite /bernoulli/=. + under eq_integral do rewrite measure_addE/=. + rewrite ge0_integralD//=. + rewrite /mscale/=. + apply: emeasurable_funM => //. + by apply/EFin_measurable_fun; exact: measurable_ubeta_nat_pdf. + apply/EFin_measurable_fun => /=. + apply: measurable_funM => //. + apply: measurable_funB => //. + exact: measurable_ubeta_nat_pdf. rewrite measure_addE. congr adde. rewrite [in LHS]/mscale/= [in RHS]/mscale/=. suff: (\int[ubeta_nat a b]_x ((ubeta_nat_pdf a' b' x)%:E) = (B (a + a') (b + b'))%:E :> \bar R)%E. + move=> <-. + rewrite muleC. + rewrite -ge0_integralZl//=. + by under eq_integral do rewrite muleC. + apply/EFin_measurable_fun. + exact: measurable_ubeta_nat_pdf. + move=> x _. + rewrite lee_fin ubeta_nat_pdf_ge0//. admit. transitivity ( (* calculating distribution (13) *) \int[mu]_(x in `[(0:R)%R, (1:R)%R]%classic) @@ -448,13 +589,18 @@ congr adde. transitivity ( (\int[mu]_(x in `[0%R, 1%R]) ((x ^+ (a + a').-1 * `1-x ^+ (b + b').-1)%:E))%E : \bar R). + apply: eq_integral => /= t. + rewrite inE/= in_itv/= => /andP[t0 t1]. + congr EFin. admit. rewrite beta_nat_normE//. rewrite /ubeta_nat_pdf//. + rewrite /ubeta_nat_pdf'. admit. rewrite [in LHS]/mscale/= [in RHS]/mscale/=. suff: (\int[ubeta_nat a b]_x (`1-(ubeta_nat_pdf a' b' x))%:E = - (B a b - B (a + a'.-1) (b + b'.-1))%:E :> \bar R)%E. + (B a b - B ((a + a')) ((b + b')))%:E :> \bar R)%E. + rewrite /Baa'bb'Bab. admit. under eq_integral do rewrite EFinB/=. rewrite integralB_EFin//=; last 2 first. @@ -469,6 +615,7 @@ congr (_ - _)%E. admit. rewrite integral_ubeta_nat//. rewrite beta_nat_normE /ubeta_nat_pdf. +rewrite /ubeta_nat_pdf'. under eq_integral => x _. rewrite -mulrA -mulrCA !mulrA/= -exprD -mulrA [X in _ * X]mulrC -exprD. over. diff --git a/theories/lang_syntax_examples_wip.v b/theories/lang_syntax_examples_wip.v index 2452c1ea2..0733181b2 100644 --- a/theories/lang_syntax_examples_wip.v +++ b/theories/lang_syntax_examples_wip.v @@ -334,23 +334,29 @@ Qed. Lemma exec_beta_a1 U : @execP R [::] _ [let "p" := Sample {exp_beta 6 4} in Sample {exp_bernoulli_trunc [#{"p"}]}] tt U = - @execP R [::] _ [Sample {exp_bernoulli_trunc [{3 / 5}:R]}] tt U. + @execP R [::] _ [Sample {exp_bernoulli_trunc [{7 / 55 (* TODO: 3 / 5 *)}:R]}] tt U. Proof. rewrite execP_letin !execP_sample execD_beta_nat !execD_bernoulli_trunc/=. rewrite !execD_real/= exp_var'E (execD_var_erefl "p")/=. -transitivity (beta_nat_bern 6 4 1 0 tt U : \bar R). +transitivity (beta_nat_bern 6 4 2 1 tt U : \bar R). rewrite /beta_nat_bern !letin'E/= /ubeta_nat_pdf/= /onem. apply: eq_integral => x _. + rewrite /=. do 2 f_equal. - by rewrite (mul1r, mulr1). + by rewrite /ubeta_nat_pdf' expr1 expr0 mulr1. rewrite beta_nat_bern_bern/= /bernoulli/= bernoulli_truncE; last by lra. rewrite measure_addE/= /mscale/=. -congr (_ * _ + _ * _)%:E; rewrite /onem; rewrite /Baa'bb'Bab /beta_nat_norm/=; -by rewrite !factS/= fact0; field. +congr (_ * _ + _ * _)%:E. + rewrite /Baa'bb'Bab /beta_nat_norm/=. + rewrite !factS/= fact0. + by field. +rewrite /onem; rewrite /Baa'bb'Bab /beta_nat_norm/=; +rewrite !factS/= fact0. +by field. Qed. Definition casino0 : @exp R _ [::] _ := - [Normalize + [Normalize let "p" := Sample {exp_uniform 0 1 a01} in let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in @@ -434,24 +440,29 @@ Definition casino3 : @exp R _ [::] _ := Lemma casino34' U : @execP R [::] _ [let "p" := Sample {exp_beta 6 4} in Sample {exp_bernoulli_trunc [{[{1}:R - #{"p"}]} ^+ {3%nat}]}] tt U = - @execP R [::] _ [Sample {exp_bernoulli_trunc [{1 / 11}:R]}] tt U. + @execP R [::] _ [Sample {exp_bernoulli_trunc [{3 / 143 (* TODO: 1 / 11*)}:R]}] tt U. Proof. rewrite execP_letin !execP_sample execD_beta_nat !execD_bernoulli_trunc/=. rewrite execD_pow/= (@execD_bin _ _ binop_minus) !execD_real/=. rewrite exp_var'E (execD_var_erefl "p")/=. -transitivity (beta_nat_bern 6 4 0 3 tt U : \bar R). +transitivity (beta_nat_bern 6 4 1 4 tt U : \bar R). rewrite /beta_nat_bern !letin'E/= /ubeta_nat_pdf/= /onem. apply: eq_integral => x _. do 2 f_equal. + rewrite /ubeta_nat_pdf'. + rewrite expr0. by rewrite mul1r. rewrite bernoulli_truncE; last by lra. rewrite beta_nat_bern_bern/= /bernoulli/=. rewrite measure_addE/= /mscale/=. -by congr (_ * _ + _ * _)%:E; rewrite /onem; -rewrite /Baa'bb'Bab /beta_nat_norm/=; rewrite !factS/= fact0; field. +congr (_ * _ + _ * _)%:E; rewrite /onem. + rewrite /Baa'bb'Bab /beta_nat_norm/=. + by rewrite !factS/= fact0; field. +rewrite /Baa'bb'Bab /beta_nat_norm/=. +by rewrite !factS/= fact0; field. Qed. -Lemma bern_onem (f : _ -> R) U p : +Lemma bern_onem (f : _ -> R) U p : (\int[beta_nat 6 4]_y bernoulli_trunc (f y) U = p%:E * \d_true U + `1-p%:E * \d_false U)%E -> (\int[beta_nat 6 4]_y bernoulli_trunc (1 - f y) U = `1-p%:E * \d_true U + p%:E * \d_false U)%E. Proof. @@ -465,7 +476,7 @@ Admitted. Definition casino4 : @exp R _ [::] _ := [Normalize let "_" := Score {1 / 9}:R in - Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. + Sample {exp_bernoulli_trunc [{140 / 143(*TODO: 10 / 11*)}:R]}]. Lemma casino34 : execD casino3 = execD casino4. @@ -480,17 +491,22 @@ rewrite !execD_real/= exp_var'E (execD_var_erefl "p")/=. transitivity (\int[beta_nat 6 4]_y bernoulli_trunc (1 - (1 - y) ^+ 3) U : \bar R)%E. by rewrite /beta_nat_bern !letin'E/= /onem. rewrite bernoulli_truncE; last by lra. -have -> := (@bern_onem (fun x => (1 - x) ^+ 3) U (1 / 11) _). +have -> := (@bern_onem (fun x => (1 - x) ^+ 3) U (3 / 143) (*TODO: (1 / 11)*) _). congr (_ * _ + _ * _)%E; congr _%:E; rewrite /onem; lra. -transitivity (beta_nat_bern 6 4 0 3 tt U : \bar R). +transitivity (beta_nat_bern 6 4 1 4 tt U : \bar R). rewrite /beta_nat_bern !letin'E/= /ubeta_nat_pdf/= /onem. apply: eq_integral => y0 _. do 2 f_equal. + rewrite /ubeta_nat_pdf'/=. + rewrite expr0. by rewrite mul1r. rewrite beta_nat_bern_bern/= /bernoulli/=. rewrite measure_addE/= /mscale/=. -by congr (_ * _ + _ * _)%:E; rewrite /onem; -rewrite /Baa'bb'Bab /beta_nat_norm/=; rewrite !factS/= fact0; field. +congr (_ * _ + _ * _)%:E; rewrite /onem. + rewrite /Baa'bb'Bab /beta_nat_norm/=. + by rewrite !factS/= fact0; field. +rewrite /Baa'bb'Bab /beta_nat_norm/=. +by rewrite !factS/= fact0; field. Qed. Lemma norm_score_bern g p1 p2 (p10 : p1 != 0) (p1_ge0 : 0 <= p1) @@ -529,10 +545,13 @@ by rewrite muleC muleA -EFinM mulVf// invr1 /onem !(mul1r, mule1). Qed. Definition casino5 : @exp R _ [::] _ := - [Normalize Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. + [Normalize Sample {exp_bernoulli_trunc [{(*TODO: 10 / 11*) 140 /143}:R]}]. Lemma casino45 : execD casino4 = execD casino5. -Proof. by rewrite norm_score_bern//; lra. Qed. +Proof. +rewrite norm_score_bern//. +lra. +Qed. End casino_example. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 9168dd1d8..717ee4aae 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -598,14 +598,41 @@ Qed. End integral_uniform. +(* normalization constant *) +Section beta_nat_norm. +Context {R : realType} (a b : nat). + +Definition beta_nat_norm : R := + (a.-1`!%:R * b.-1`!%:R / (a + b).-1`!%:R)%R. + +Lemma beta_nat_norm_gt0 : (0 < beta_nat_norm :> R)%R. +Proof. +by rewrite /beta_nat_norm divr_gt0// ?mulr_gt0 ?ltr0n ?fact_gt0. +Qed. + +Lemma beta_nat_norm_ge0 : (0 <= beta_nat_norm :> R)%R. +Proof. exact/ltW/beta_nat_norm_gt0. Qed. + +End beta_nat_norm. + +Lemma beta_nat_norm00 {R : realType} : beta_nat_norm 0 0 = 1%R :> R. +Proof. by rewrite /beta_nat_norm/= fact0 mulr1/= divff. Qed. + +Lemma beta_nat_norm11 {R : realType} : beta_nat_norm 1 1 = 1%R :> R. +Proof. by rewrite /beta_nat_norm/= fact0 mulr1/= divff. Qed. + (* definition of the beta probability specialized to natural numbers *) + +Definition ubeta_nat_pdf' {R : realType} (a b : nat) (t : R) := + (t ^+ a * (`1- t) ^+ b)%R. + Section beta_probability. Local Open Scope ring_scope. Context {R : realType}. Variables a b : nat. - (* unnormalized pdf for beta specialized to nat *) -Definition ubeta_nat_pdf (t : R) := t ^+ a * (`1- t) ^+ b. + +Definition ubeta_nat_pdf (t : R) := ubeta_nat_pdf' a.-1 b.-1 t. Lemma ubeta_nat_pdf_ge0 t : 0 <= t <= 1 -> 0 <= ubeta_nat_pdf t. Proof. by move=> /andP[t0 t1]; rewrite mulr_ge0// exprn_ge0// onem_ge0. Qed. @@ -621,24 +648,15 @@ Proof. by apply /measurable_funM => //; exact/measurable_fun_pow/measurable_funB. Qed. -(* normalization constant *) -Definition beta_nat_norm : R := a.-1`!%:R * b.-1`!%:R / (a + b).-1`!%:R. - Lemma beta_nat_normE : - beta_nat_norm = \int[lebesgue_measure]_(x in `[0, 1]) ubeta_nat_pdf x. + beta_nat_norm a b = \int[lebesgue_measure]_(x in `[0, 1]) ubeta_nat_pdf x. Proof. +rewrite /beta_nat_norm. +rewrite /ubeta_nat_pdf /ubeta_nat_pdf'. Admitted. -Lemma beta_nat_norm_gt0 : 0 < beta_nat_norm. -Proof. -by rewrite /beta_nat_norm divr_gt0// ?mulr_gt0 ?ltr0n ?fact_gt0. -Qed. - -Lemma beta_nat_norm_ge0 : 0 <= beta_nat_norm. -Proof. exact/ltW/beta_nat_norm_gt0. Qed. - (* normalized pdf for beta specialized to nat *) -Definition beta_nat_pdf t := ubeta_nat_pdf t / beta_nat_norm. +Definition beta_nat_pdf t := ubeta_nat_pdf t / (beta_nat_norm a b). Lemma measurable_beta_nat_pdf : measurable_fun setT beta_nat_pdf. Proof. by apply: measurable_funM => //; exact: measurable_ubeta_nat_pdf. Qed. @@ -676,10 +694,12 @@ apply: cvg_toP. apply: ereal_nondecreasing_is_cvgn => m n mn. admit. rewrite ge0_integral_bigcup//=. -admit. -admit. -admit. -admit. +- by move=> k; exact: measurableI. +- admit. +- move=> /= r [i _ [Fir]] /=. + rewrite in_itv/= => r01. + by rewrite lee_fin ubeta_nat_pdf_ge0. +- exact: trivIset_setIr. Admitted. HB.instance Definition _ := isMeasure.Build _ _ _ ubeta_nat @@ -687,7 +707,7 @@ HB.instance Definition _ := isMeasure.Build _ _ _ ubeta_nat Definition beta_nat (*: set [the measurableType (R.-ocitv.-measurable).-sigma of salgebraType R.-ocitv.-measurable] -> \bar R*) := - @mscale _ _ _ (invr_nonneg (NngNum beta_nat_norm_ge0)) ubeta_nat. + @mscale _ _ _ (invr_nonneg (NngNum (beta_nat_norm_ge0 a b))) ubeta_nat. Let beta_nat0 : beta_nat set0 = 0. Proof. exact: measure0. Qed. @@ -724,7 +744,6 @@ rewrite beta_nat_normE /ubeta_nat setTI. Admitted. HB.instance Definition _ := @Measure_isProbability.Build _ _ _ - beta_nat beta_nat_setT. Lemma beta_nat01 : beta_nat `[0, 1] = 1%:E. @@ -740,16 +759,13 @@ Section beta_probability11. Local Open Scope ring_scope. Context {R : realType}. -Lemma ubeta_nat_pdf11 : ubeta_nat_pdf 0 0 = @cst R _ 1. -Proof. by apply/funext => r; rewrite /ubeta_nat_pdf/= !expr0 mulr1. Qed. - -Lemma beta_nat_norm11 : beta_nat_norm 0 0 = 1 :> R. -Proof. by rewrite /beta_nat_norm/= fact0 mulr1/= divff. Qed. +Lemma ubeta_nat_pdf11 : ubeta_nat_pdf 1 1 = @cst R _ 1. +Proof. by apply/funext => r; rewrite /ubeta_nat_pdf/= /ubeta_nat_pdf' !expr0 mulr1. Qed. Let a01 : 0 < 1 - 0 :> R. Proof. by []. Qed. Lemma beta11_uniform U : measurable U -> - beta_nat 0 0 U = uniform_probability a01 U. + beta_nat 1 1 U = uniform_probability a01 U. Proof. move=> mU; rewrite /beta_nat /uniform_probability. rewrite /mscale/= beta_nat_norm11 subr0 invr1 !mul1e. From 93e42acf57eee99bbc78b53cc59877e74883021e Mon Sep 17 00:00:00 2001 From: AyumuSaito Date: Tue, 27 Feb 2024 20:43:09 +0900 Subject: [PATCH 23/36] fix --- theories/lang_syntax.v | 11 ++++++----- theories/lang_syntax_examples_wip.v | 16 ++++++++-------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index 6e868ef9d..79e59509d 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -422,7 +422,7 @@ Import Notations. Program Definition beta_nat_bern : R.-sfker munit ~> mbool := @letin' _ _ _ munit _ mbool _ (sample_cst [the probability _ _ of beta_nat a b]) - (sample (bernoulli_trunc \o ubeta_nat_pdf a' b' \o @fst (measurableTypeR R) _ ) _). + (sample (bernoulli_trunc \o ubeta_nat_pdf a'.+1 b'.+1 \o @fst (measurableTypeR R) _ ) _). Next Obligation. apply: measurableT_comp. apply: measurableT_comp. @@ -492,7 +492,7 @@ rewrite subr_ge0. exact: Baa'bb'Bab_le1. Qed. -Lemma ubeta_nat_pdf_ge0' t : 0 <= ubeta_nat_pdf a' b' t :> R. +Lemma ubeta_nat_pdf_ge0' t : 0 <= ubeta_nat_pdf a'.+1 b'.+1 t :> R. Proof. apply: ubeta_nat_pdf_ge0. (* TODO: needs 0 <= t <= 1 *) Admitted. @@ -505,10 +505,11 @@ rewrite /ubeta_nat_pdf'. (* TODO: needs 0 <= t <= 1 *) Admitted. Lemma integral_ubeta_nat : - (\int[ubeta_nat a b]_x (ubeta_nat_pdf a' b' x)%:E = + (\int[ubeta_nat a b]_x (ubeta_nat_pdf a'.+1 b'.+1 x)%:E = \int[mu]_(x in `[0%R, 1%R]) (x ^+ a'.-1 * `1-x ^+ b'.-1 * x ^+ a * `1-x ^+ b)%:E :> \bar R)%E. Proof. +rewrite /ubeta_nat/ubeta_nat_pdf. Admitted. Lemma beta_nat_bern_bern U : @@ -570,7 +571,7 @@ transitivity ( rewrite measure_addE. congr adde. rewrite [in LHS]/mscale/= [in RHS]/mscale/=. - suff: (\int[ubeta_nat a b]_x ((ubeta_nat_pdf a' b' x)%:E) + suff: (\int[ubeta_nat a b]_x ((ubeta_nat_pdf a'.+1 b'.+1 x)%:E) = (B (a + a') (b + b'))%:E :> \bar R)%E. move=> <-. rewrite muleC. @@ -598,7 +599,7 @@ congr adde. rewrite /ubeta_nat_pdf'. admit. rewrite [in LHS]/mscale/= [in RHS]/mscale/=. -suff: (\int[ubeta_nat a b]_x (`1-(ubeta_nat_pdf a' b' x))%:E = +suff: (\int[ubeta_nat a b]_x (`1-(ubeta_nat_pdf a'.+1 b'.+1 x))%:E = (B a b - B ((a + a')) ((b + b')))%:E :> \bar R)%E. rewrite /Baa'bb'Bab. admit. diff --git a/theories/lang_syntax_examples_wip.v b/theories/lang_syntax_examples_wip.v index 0733181b2..fc74b7e3b 100644 --- a/theories/lang_syntax_examples_wip.v +++ b/theories/lang_syntax_examples_wip.v @@ -334,11 +334,11 @@ Qed. Lemma exec_beta_a1 U : @execP R [::] _ [let "p" := Sample {exp_beta 6 4} in Sample {exp_bernoulli_trunc [#{"p"}]}] tt U = - @execP R [::] _ [Sample {exp_bernoulli_trunc [{7 / 55 (* TODO: 3 / 5 *)}:R]}] tt U. + @execP R [::] _ [Sample {exp_bernoulli_trunc [{3 / 5}:R]}] tt U. Proof. rewrite execP_letin !execP_sample execD_beta_nat !execD_bernoulli_trunc/=. rewrite !execD_real/= exp_var'E (execD_var_erefl "p")/=. -transitivity (beta_nat_bern 6 4 2 1 tt U : \bar R). +transitivity (beta_nat_bern 6 4 1 0 tt U : \bar R). rewrite /beta_nat_bern !letin'E/= /ubeta_nat_pdf/= /onem. apply: eq_integral => x _. rewrite /=. @@ -440,12 +440,12 @@ Definition casino3 : @exp R _ [::] _ := Lemma casino34' U : @execP R [::] _ [let "p" := Sample {exp_beta 6 4} in Sample {exp_bernoulli_trunc [{[{1}:R - #{"p"}]} ^+ {3%nat}]}] tt U = - @execP R [::] _ [Sample {exp_bernoulli_trunc [{3 / 143 (* TODO: 1 / 11*)}:R]}] tt U. + @execP R [::] _ [Sample {exp_bernoulli_trunc [{1 / 11}:R]}] tt U. Proof. rewrite execP_letin !execP_sample execD_beta_nat !execD_bernoulli_trunc/=. rewrite execD_pow/= (@execD_bin _ _ binop_minus) !execD_real/=. rewrite exp_var'E (execD_var_erefl "p")/=. -transitivity (beta_nat_bern 6 4 1 4 tt U : \bar R). +transitivity (beta_nat_bern 6 4 0 3 tt U : \bar R). rewrite /beta_nat_bern !letin'E/= /ubeta_nat_pdf/= /onem. apply: eq_integral => x _. do 2 f_equal. @@ -476,7 +476,7 @@ Admitted. Definition casino4 : @exp R _ [::] _ := [Normalize let "_" := Score {1 / 9}:R in - Sample {exp_bernoulli_trunc [{140 / 143(*TODO: 10 / 11*)}:R]}]. + Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. Lemma casino34 : execD casino3 = execD casino4. @@ -491,9 +491,9 @@ rewrite !execD_real/= exp_var'E (execD_var_erefl "p")/=. transitivity (\int[beta_nat 6 4]_y bernoulli_trunc (1 - (1 - y) ^+ 3) U : \bar R)%E. by rewrite /beta_nat_bern !letin'E/= /onem. rewrite bernoulli_truncE; last by lra. -have -> := (@bern_onem (fun x => (1 - x) ^+ 3) U (3 / 143) (*TODO: (1 / 11)*) _). +have -> := (@bern_onem (fun x => (1 - x) ^+ 3) U (1 / 11) _). congr (_ * _ + _ * _)%E; congr _%:E; rewrite /onem; lra. -transitivity (beta_nat_bern 6 4 1 4 tt U : \bar R). +transitivity (beta_nat_bern 6 4 0 3 tt U : \bar R). rewrite /beta_nat_bern !letin'E/= /ubeta_nat_pdf/= /onem. apply: eq_integral => y0 _. do 2 f_equal. @@ -545,7 +545,7 @@ by rewrite muleC muleA -EFinM mulVf// invr1 /onem !(mul1r, mule1). Qed. Definition casino5 : @exp R _ [::] _ := - [Normalize Sample {exp_bernoulli_trunc [{(*TODO: 10 / 11*) 140 /143}:R]}]. + [Normalize Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. Lemma casino45 : execD casino4 = execD casino5. From 718933401c3e0b333ab7c41a2ca450edc5e5a3dd Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Fri, 1 Mar 2024 16:46:29 +0900 Subject: [PATCH 24/36] minor progress --- theories/lang_syntax.v | 12 +++------- theories/prob_lang.v | 54 +++++++++++++++++++++++++----------------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index 79e59509d..6fa131ccf 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -581,7 +581,7 @@ congr adde. exact: measurable_ubeta_nat_pdf. move=> x _. rewrite lee_fin ubeta_nat_pdf_ge0//. - admit. + admit. (* 0 <= x <= 1 *) transitivity ( (* calculating distribution (13) *) \int[mu]_(x in `[(0:R)%R, (1:R)%R]%classic) ((x ^+ a'.-1 * (`1- x) ^+ b'.-1 * x ^+ a * (`1- x) ^+ b)%:E) @@ -594,10 +594,7 @@ congr adde. rewrite inE/= in_itv/= => /andP[t0 t1]. congr EFin. admit. - rewrite beta_nat_normE//. - rewrite /ubeta_nat_pdf//. - rewrite /ubeta_nat_pdf'. - admit. + by rewrite beta_nat_normE. rewrite [in LHS]/mscale/= [in RHS]/mscale/=. suff: (\int[ubeta_nat a b]_x (`1-(ubeta_nat_pdf a'.+1 b'.+1 x))%:E = (B a b - B ((a + a')) ((b + b')))%:E :> \bar R)%E. @@ -608,12 +605,10 @@ rewrite integralB_EFin//=; last 2 first. admit. admit. rewrite integral_cst//= mul1e. -rewrite beta_nat_normE. rewrite {1}/ubeta_nat setTI. rewrite EFinB. congr (_ - _)%E. - (* rewrite fineK//. *) - admit. + by rewrite -beta_nat_normE. rewrite integral_ubeta_nat//. rewrite beta_nat_normE /ubeta_nat_pdf. rewrite /ubeta_nat_pdf'. @@ -621,7 +616,6 @@ under eq_integral => x _. rewrite -mulrA -mulrCA !mulrA/= -exprD -mulrA [X in _ * X]mulrC -exprD. over. rewrite /=. -admit. Admitted. End casino3_casino4. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 717ee4aae..42fd07c6a 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -649,10 +649,11 @@ by apply /measurable_funM => //; exact/measurable_fun_pow/measurable_funB. Qed. Lemma beta_nat_normE : - beta_nat_norm a b = \int[lebesgue_measure]_(x in `[0, 1]) ubeta_nat_pdf x. + (beta_nat_norm a b)%:E = (\int[lebesgue_measure]_(x in `[0%R, 1%R]) (ubeta_nat_pdf x)%:E)%E. Proof. rewrite /beta_nat_norm. -rewrite /ubeta_nat_pdf /ubeta_nat_pdf'. +rewrite /ubeta_nat_pdf. +rewrite /ubeta_nat_pdf'. Admitted. (* normalized pdf for beta specialized to nat *) @@ -684,23 +685,34 @@ rewrite /ubeta_nat integral_ge0//= => x [Ux]. by rewrite in_itv/= => x01; rewrite lee_fin ubeta_nat_pdf_ge0. Qed. -(* TODO: doable *) +(* TODO: should be shorter *) Let ubeta_nat_sigma_additive : semi_sigma_additive ubeta_nat. Proof. -move=> /= F mF tF mUF. -rewrite /ubeta_nat. -rewrite setI_bigcupl. -apply: cvg_toP. +move=> /= F mF tF mUF; rewrite /ubeta_nat setI_bigcupl; apply: cvg_toP. apply: ereal_nondecreasing_is_cvgn => m n mn. - admit. + apply: lee_sum_nneg_natr => // k _ _. + apply: integral_ge0 => /= x [_]; rewrite in_itv => x01. + by rewrite lee_fin; exact: ubeta_nat_pdf_ge0. rewrite ge0_integral_bigcup//=. - by move=> k; exact: measurableI. -- admit. -- move=> /= r [i _ [Fir]] /=. - rewrite in_itv/= => r01. +- apply/integrableP; split. + by apply/EFin_measurable_fun; exact: measurable_funTS measurable_ubeta_nat_pdf. + apply: le_lt_trans => /=. + apply: (@subset_integral _ _ _ mu _ `[0%R, 1%R]) => //=. + - rewrite -setI_bigcupl; apply: measurableI => //. + - apply/measurableT_comp => //; apply/measurableT_comp => //. + exact: measurable_funTS measurable_ubeta_nat_pdf. + - by apply: bigcup_sub => k _; exact: subIsetr. + rewrite /=. + under eq_integral. + move=> /= x; rewrite inE/= in_itv/= => x01. + rewrite ger0_norm//; last by rewrite ubeta_nat_pdf_ge0. + over. + by rewrite -beta_nat_normE ltry. +- move=> x [k _ [_]]; rewrite /= in_itv/= => x01. by rewrite lee_fin ubeta_nat_pdf_ge0. - exact: trivIset_setIr. -Admitted. +Qed. HB.instance Definition _ := isMeasure.Build _ _ _ ubeta_nat ubeta_nat0 ubeta_nat_ge0 ubeta_nat_sigma_additive. @@ -736,22 +748,20 @@ HB.instance Definition _ := isMeasure.Build _ _ _ beta_nat beta_nat0 beta_nat_ge Let beta_nat_setT : beta_nat setT = 1%:E. Proof. -rewrite /beta_nat. -rewrite /=. -rewrite /mscale /=. -rewrite beta_nat_normE /ubeta_nat setTI. -(* TODO: doable *) -Admitted. +rewrite /beta_nat /= /mscale /=. +rewrite /ubeta_nat/= setTI. +by rewrite -beta_nat_normE -EFinM mulVr// unitfE gt_eqF// beta_nat_norm_gt0. +Qed. HB.instance Definition _ := @Measure_isProbability.Build _ _ _ beta_nat beta_nat_setT. Lemma beta_nat01 : beta_nat `[0, 1] = 1%:E. Proof. -rewrite /beta_nat. -rewrite /mscale/= /beta_nat /ubeta_nat. -rewrite beta_nat_normE setIid. -Admitted. +rewrite /beta_nat /= /mscale/=. +rewrite /beta_nat /ubeta_nat setIidr//. +by rewrite -beta_nat_normE -EFinM mulVr// unitfE gt_eqF// beta_nat_norm_gt0. +Qed. End beta_probability. From 1fe3568d503e99245ecd4a4de81a2324a6fc8662 Mon Sep 17 00:00:00 2001 From: saito ayumu Date: Wed, 6 Mar 2024 18:13:46 +0900 Subject: [PATCH 25/36] casion example (wip) - use change of variables from charge.v to prove lemma integral_beta_nat (wip) - beta_nat_bern_bern --- theories/lang_syntax.v | 274 ---------------- theories/lang_syntax_examples_wip.v | 361 ++++++++++++++------- theories/prob_lang.v | 465 ++++++++++++++++++++++++---- 3 files changed, 661 insertions(+), 439 deletions(-) diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index 6fa131ccf..27ef25966 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -346,280 +346,6 @@ End accessor_functions. Arguments acc_typ {R} s n. Arguments measurable_acc_typ {R} s n. - -Lemma factD n m : (n`! * m`! <= (n + m).+1`!)%N. -Proof. -elim: n m => /= [m|n ih m]. - by rewrite fact0 mul1n add0n factS leq_pmull. -rewrite 2!factS [in X in (_ <= _ * X)%N]addSn -mulnA leq_mul//. -by rewrite ltnS addSnnS leq_addr. -Qed. - -Lemma factD' n m : (n`! * m.-1`! <= (n + m)`!)%N. -Proof. -case: m => //= [|m]. - by rewrite fact0 muln1 addn0. -by rewrite addnS factD. -Qed. - -Lemma leq_prod2 (x y n m : nat) : (n <= x)%N -> (m <= y)%N -> - (\prod_(m <= i < y) i * \prod_(n <= i < x) i <= \prod_(n + m <= i < x + y) i)%N. -Proof. -move=> nx my. -rewrite big_addn. -rewrite -addnBA//. -rewrite {3}/index_iota. -rewrite -addnBAC//. -rewrite iotaD. -rewrite big_cat/=. -rewrite mulnC. -rewrite leq_mul//. - rewrite /index_iota. - apply: leq_prod. - by move=> i _; rewrite leq_addr. -rewrite subnKC//. -rewrite -{1}(add0n m). -rewrite big_addn. -rewrite {2}(_ : (y - m) = ((y - m + x) - x))%N; last first. - by rewrite -addnBA// subnn addn0. -rewrite -{1}(add0n x). -rewrite big_addn. -rewrite -addnBA// subnn addn0. -apply: leq_prod => i _. -by rewrite leq_add2r leq_addr. -Qed. - -Lemma leq_fact2 (x y n m : nat) : - (n <= x) %N -> (m <= y)%N -> - (x`! * y`! * ((n + m).+1)`! <= n`! * m`! * ((x + y).+1)`!)%N. -Proof. -move=> nx my. -rewrite (_ : x`! = n`! * \prod_(n.+1 <= i < x.+1) i)%N; last first. - by rewrite -fact_split. -rewrite -!mulnA leq_mul2l; apply/orP; right. -rewrite (_ : y`! = m`! * \prod_(m.+1 <= i < y.+1) i)%N; last first. - by rewrite -fact_split. -rewrite mulnCA -!mulnA leq_mul2l; apply/orP; right. -rewrite (_ : (x + y).+1`! = (n + m).+1`! * \prod_((n + m).+2 <= i < (x + y).+2) i)%N; last first. - rewrite -fact_split//. - by rewrite ltnS leq_add. -rewrite mulnA mulnC leq_mul2l; apply/orP; right. -rewrite -addSn -addnS. -rewrite -addSn -addnS. -exact: leq_prod2. -Qed. - -(* part of the step from casino3 to casino4 *) -Section casino3_casino4. -Context {R : realType}. -Variables a b a' b' : nat. - -Local Notation mu := lebesgue_measure. - -Open Scope ring_scope. -Import Notations. - -Program Definition beta_nat_bern : R.-sfker munit ~> mbool := - @letin' _ _ _ munit _ mbool _ - (sample_cst [the probability _ _ of beta_nat a b]) - (sample (bernoulli_trunc \o ubeta_nat_pdf a'.+1 b'.+1 \o @fst (measurableTypeR R) _ ) _). -Next Obligation. -apply: measurableT_comp. - apply: measurableT_comp. - exact: measurable_bernoulli_trunc. - exact: measurable_ubeta_nat_pdf. -exact: (measurable_acc_typ [:: Real] 0). -Qed. - -Local Notation B := beta_nat_norm. - -Definition Baa'bb'Bab : R := (beta_nat_norm (a + a') (b + b')) / beta_nat_norm a b. - -Lemma Baa'bb'Bab_ge0 : 0 <= Baa'bb'Bab. -Proof. by rewrite /Baa'bb'Bab divr_ge0// beta_nat_norm_ge0. Qed. - -Definition Baa'bb'Bab_nneg : {nonneg R} := NngNum Baa'bb'Bab_ge0. - -Lemma Baa'bb'Bab_le1 : Baa'bb'Bab_nneg%:num <= 1. -Proof. -rewrite /Baa'bb'Bab_nneg/= /Baa'bb'Bab. -rewrite ler_pdivrMr// ?mul1r ?beta_nat_norm_gt0//. -rewrite /B /beta_nat_norm. -rewrite ler_pdivrMr ?ltr0n ?fact_gt0//. -rewrite mulrAC. -rewrite ler_pdivlMr ?ltr0n ?fact_gt0//. -rewrite -!natrM ler_nat. -case: a. - rewrite /= fact0 mul1n !add0n. - case: b => /=. - case: a' => //. - case: b' => //= m. - by rewrite fact0 !mul1n muln1. - move=> n/=. - by rewrite fact0 add0n muln1 mul1n factD'. - move=> m. - rewrite mulnC leq_mul// mulnC. - by rewrite (leq_trans (factD' _ _))// addSn addnS//= addnC. -move=> n. -rewrite addSn. -case: b. - rewrite !fact0 add0n muln1 [leqRHS]mulnC addn0/= leq_mul//. - by rewrite factD'. -move=> m. -clear a b. -rewrite [(n + a').+1.-1]/=. -rewrite [n.+1.-1]/=. -rewrite [m.+1.-1]/=. -rewrite addnS. -rewrite [(_ + m).+1.-1]/=. -rewrite (addSn m b'). -rewrite [(m + _).+1.-1]/=. -rewrite (addSn (n + a')). -rewrite [_.+1.-1]/=. -rewrite addSn addnS. -by rewrite leq_fact2// leq_addr. -Qed. - -Lemma onem_Baa'bb'Bab_ge0 : 0 <= 1 - (Baa'bb'Bab_nneg%:num). -Proof. by rewrite subr_ge0 Baa'bb'Bab_le1. Qed. - -Lemma onem_Baa'bb'Bab_ge0_fix : 0 <= B a b * (1 - Baa'bb'Bab_nneg%:num). -Proof. -rewrite mulr_ge0//. - rewrite /B. - exact: beta_nat_norm_ge0. -rewrite subr_ge0. -exact: Baa'bb'Bab_le1. -Qed. - -Lemma ubeta_nat_pdf_ge0' t : 0 <= ubeta_nat_pdf a'.+1 b'.+1 t :> R. -Proof. -apply: ubeta_nat_pdf_ge0. (* TODO: needs 0 <= t <= 1 *) -Admitted. - -Lemma ubeta_nat_pdf_le1' t : (NngNum (ubeta_nat_pdf_ge0' t))%:num <= 1 :> R. -Proof. -rewrite /=. -rewrite /ubeta_nat_pdf. -rewrite /ubeta_nat_pdf'. (* TODO: needs 0 <= t <= 1 *) -Admitted. - -Lemma integral_ubeta_nat : - (\int[ubeta_nat a b]_x (ubeta_nat_pdf a'.+1 b'.+1 x)%:E = - \int[mu]_(x in `[0%R, 1%R]) - (x ^+ a'.-1 * `1-x ^+ b'.-1 * x ^+ a * `1-x ^+ b)%:E :> \bar R)%E. -Proof. -rewrite /ubeta_nat/ubeta_nat_pdf. -Admitted. - -Lemma beta_nat_bern_bern U : - beta_nat_bern tt U = - sample_cst (bernoulli Baa'bb'Bab_le1) tt U. -Proof. -rewrite /beta_nat_bern. -rewrite [LHS]letin'E/=. -transitivity ((\int[beta_nat a b]_y - (@bernoulli R (NngNum (ubeta_nat_pdf_ge0' y)) (ubeta_nat_pdf_le1' y) U))%E). - apply: eq_integral => /= r _. - rewrite /bernoulli_trunc/=. - case: sumbool_ler; last first. - admit. - move=> H_ge0. - case: sumbool_ler; last first. - admit. - move=> H_le1. - rewrite /=/bernoulli !measure_addE/= /mscale/=. - by congr _%E. -rewrite /beta_nat/=. -transitivity (((B a b)^-1%:E * \int[ubeta_nat a b]_x bernoulli (ubeta_nat_pdf_le1' x) U)%E). - rewrite ge0_integral_mscale //=. - admit. -rewrite {2}/bernoulli. -suff: (\int[ubeta_nat a b]_x bernoulli (ubeta_nat_pdf_le1' x) U)%E = - measure_add - (mscale (NngNum (beta_nat_norm_ge0 (a + a') (b + b'))) \d_true) - (mscale (NngNum (onem_Baa'bb'Bab_ge0_fix)) \d_false) U. - rewrite /= => ->. - rewrite !measure_addE/= /mscale/=. - rewrite /Baa'bb'Bab/=. - rewrite muleDr//. - congr (_ + _)%E. - rewrite -EFinM. - rewrite mulrA. - rewrite (mulrC (_^-1)). - by rewrite EFinM. - rewrite muleA. - congr (_ * _)%E. - rewrite -EFinM. - rewrite mulrA mulVr ?mul1r ?unitfE//. - rewrite gt_eqF//. - by rewrite beta_nat_norm_gt0. -transitivity ( - \int[ubeta_nat a b]_x (mscale (NngNum (ubeta_nat_pdf_ge0' x)) \d_true U) - + - \int[ubeta_nat a b]_x (mscale (onem_nonneg (ubeta_nat_pdf_le1' x)) \d_false) U)%E. - rewrite /bernoulli/=. - under eq_integral do rewrite measure_addE/=. - rewrite ge0_integralD//=. - rewrite /mscale/=. - apply: emeasurable_funM => //. - by apply/EFin_measurable_fun; exact: measurable_ubeta_nat_pdf. - apply/EFin_measurable_fun => /=. - apply: measurable_funM => //. - apply: measurable_funB => //. - exact: measurable_ubeta_nat_pdf. -rewrite measure_addE. -congr adde. - rewrite [in LHS]/mscale/= [in RHS]/mscale/=. - suff: (\int[ubeta_nat a b]_x ((ubeta_nat_pdf a'.+1 b'.+1 x)%:E) - = (B (a + a') (b + b'))%:E :> \bar R)%E. - move=> <-. - rewrite muleC. - rewrite -ge0_integralZl//=. - by under eq_integral do rewrite muleC. - apply/EFin_measurable_fun. - exact: measurable_ubeta_nat_pdf. - move=> x _. - rewrite lee_fin ubeta_nat_pdf_ge0//. - admit. (* 0 <= x <= 1 *) - transitivity ( (* calculating distribution (13) *) - \int[mu]_(x in `[(0:R)%R, (1:R)%R]%classic) - ((x ^+ a'.-1 * (`1- x) ^+ b'.-1 * x ^+ a * (`1- x) ^+ b)%:E) - )%E. - by rewrite integral_ubeta_nat. - transitivity ( - (\int[mu]_(x in `[0%R, 1%R]) - ((x ^+ (a + a').-1 * `1-x ^+ (b + b').-1)%:E))%E : \bar R). - apply: eq_integral => /= t. - rewrite inE/= in_itv/= => /andP[t0 t1]. - congr EFin. - admit. - by rewrite beta_nat_normE. -rewrite [in LHS]/mscale/= [in RHS]/mscale/=. -suff: (\int[ubeta_nat a b]_x (`1-(ubeta_nat_pdf a'.+1 b'.+1 x))%:E = - (B a b - B ((a + a')) ((b + b')))%:E :> \bar R)%E. - rewrite /Baa'bb'Bab. - admit. -under eq_integral do rewrite EFinB/=. -rewrite integralB_EFin//=; last 2 first. - admit. - admit. -rewrite integral_cst//= mul1e. -rewrite {1}/ubeta_nat setTI. -rewrite EFinB. -congr (_ - _)%E. - by rewrite -beta_nat_normE. -rewrite integral_ubeta_nat//. -rewrite beta_nat_normE /ubeta_nat_pdf. -rewrite /ubeta_nat_pdf'. -under eq_integral => x _. - rewrite -mulrA -mulrCA !mulrA/= -exprD -mulrA [X in _ * X]mulrC -exprD. -over. -rewrite /=. -Admitted. - -End casino3_casino4. - Section context. Variables (R : realType). Definition ctx := seq (string * typ). diff --git a/theories/lang_syntax_examples_wip.v b/theories/lang_syntax_examples_wip.v index fc74b7e3b..6273470c0 100644 --- a/theories/lang_syntax_examples_wip.v +++ b/theories/lang_syntax_examples_wip.v @@ -1,11 +1,12 @@ Require Import String. From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. -From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. -From mathcomp.classical Require Import functions cardinality fsbigop. -Require Import signed reals ereal topology normedtype sequences esum measure. -Require Import lebesgue_measure numfun lebesgue_integral kernel prob_lang. -Require Import lang_syntax_util lang_syntax. +From mathcomp Require Import mathcomp_extra boolp classical_sets. +From mathcomp Require Import functions cardinality fsbigop. +From mathcomp Require Import signed reals ereal topology normedtype sequences. +From mathcomp Require Import esum measure charge lebesgue_measure numfun. +From mathcomp Require Import lebesgue_integral kernel prob_lang. +From mathcomp Require Import lang_syntax_util lang_syntax lang_syntax_examples. From mathcomp Require Import ring lra. (******************************************************************************) @@ -23,61 +24,86 @@ Local Open Scope classical_set_scope. Local Open Scope ring_scope. (* Local Open Scope ereal_scope. *) -Section trunc_lemmas. -Open Scope ring_scope. -Open Scope lang_scope. -Context (R : realType). - -Lemma bernoulli_truncE (p : R) U : - (0 <= p <= 1)%R -> - (bernoulli_trunc p U = - p%:E * \d_true U + (`1-p)%:E * \d_false U)%E. -Proof. -move=> /andP[p0 p1]. -rewrite /bernoulli_trunc. -case: (sumbool_ler 0 p) => [{}p0/=|]. - case: (sumbool_ler p 1) => [{}p1/=|]. - by rewrite /bernoulli/= measure_addE. - by rewrite ltNge p1. -by rewrite ltNge p0. -Qed. - -Lemma __ p p1 : - @execD R [::] _ (exp_bernoulli p p1) = - execD (exp_bernoulli_trunc [{p%:num}:R]). -Proof. -apply: eq_execD. -rewrite execD_bernoulli execD_bernoulli_trunc execD_real/=. -apply: funext=> x /=. -rewrite /bernoulli_trunc. -case: (sumbool_ler 0 p%:num) => [{}p0/=|]. - case: (sumbool_ler p%:num 1) => [{}p1'/=|]. - admit. -Abort. - -End trunc_lemmas. - Section beta_example. Open Scope ring_scope. Open Scope lang_scope. -Context (R : realType). - -Let p610 : ((6 / 10%:R)%:nng : {nonneg R})%:num <= 1. -Proof. admit. Admitted. +Context {R : realType}. -Lemma beta_bernoulli : - @execP R [::] _ [let "p" := Sample {exp_beta 6 4} in Sample {exp_bernoulli_trunc [#{"p"}]}] = - execP [Sample {@exp_bernoulli _ _ (6 / 10%:R)%:nng p610}]. +Lemma beta_bern610 : + @execP R [::] _ + [let "p" := + Sample {exp_beta 6 4} in Sample {exp_bernoulli_trunc [#{"p"}]}] = + execP [Sample {exp_bernoulli_trunc [{6 / 10}:R]}]. Proof. -rewrite execP_letin !execP_sample !execD_beta_nat !execD_bernoulli/=. -rewrite execD_bernoulli_trunc exp_var'E !(execD_var_erefl "p")/=. +rewrite execP_letin !execP_sample !execD_beta_nat !execD_bernoulli_trunc/=. +rewrite execD_real exp_var'E !(execD_var_erefl "p")/=. apply: eq_sfkernel=> x U. rewrite letin'E/=. -rewrite /beta_nat/mscale/=. -transitivity (bernoulli_trunc ((@beta_nat_norm R 7 4) / (@beta_nat_norm R 6 4)) U); last first. - (* congr (bernoulli_trunc _ _). *) - (* rewrite /beta_nat_norm/= factE/=; lra. *) -(* apply: beta_nat_bern_bern. *) +transitivity ((\int[beta_nat 6 4]_(y in `[0%R, 1%R]) bernoulli_trunc y U)%E : \bar R). + rewrite [in RHS]integral_mkcond /=. + apply: eq_integral => y _. + rewrite patchE. + case: ifPn => //. + (* Unset Printing Notations. *) + simpl in *. + rewrite mem_setE /= in_itv /= negb_and -!ltNge => /orP[y0|y1]; + rewrite /bernoulli_trunc/=. + case: sumbool_ler. + move=> a. + by rewrite ltNge a in y0. + rewrite /prob_lang.bernoulli0 /bernoulli => _. + rewrite [LHS]measure_addE/= /mscale/=. + (* match default value *) + admit. + admit. + rewrite integral_beta_nat. +under eq_integral => y y01. + rewrite bernoulli_truncE. + rewrite muleC muleDr// !muleA muleC [X in _ + X]muleC. + over. + admit. +rewrite /= bernoulli_truncE. +rewrite integralD//. +congr (_ + _)%E. +rewrite integralZl//. +rewrite muleC. +congr (_ * _)%E. +(* rewrite /= setTI. *) +rewrite /beta_nat_pdf. +rewrite /ubeta_nat_pdf/ubeta_nat_pdf'/=. +transitivity (\int[@lebesgue_measure R]_(x0 in `[0%R, 1%R]) + ((ubeta_nat_pdf 7 4 x0 / beta_nat_norm 6 4)%:E))%E. + apply: eq_integral => p _. + rewrite muleC -EFinM. + rewrite -[X in X * _]divr1 mulf_div. + congr (_ / _)%:E; last by rewrite mul1r. + by rewrite /ubeta_nat_pdf/= /ubeta_nat_pdf' mulrA -exprS. +under eq_integral do rewrite mulrC EFinM. +rewrite integralZl//=. +rewrite -beta_nat_normE /beta_nat_norm/= factE. +rewrite -!EFinM/=. +congr _%:E; lra. + admit. + admit. +rewrite integralZl//. +rewrite muleC. +congr (_ * _)%E. +rewrite /beta_nat_pdf. +rewrite /ubeta_nat_pdf/ubeta_nat_pdf'/=. +transitivity (\int[@lebesgue_measure R]_(x0 in `[0%R, 1%R]) + ((ubeta_nat_pdf 6 5 x0 / beta_nat_norm 6 4)%:E))%E. + apply: eq_integral => p _. + by rewrite mulrC -EFinM -2!mulrA -exprSr mulrC. +under eq_integral do rewrite mulrC EFinM. +rewrite integralZl//=. +rewrite -beta_nat_normE /beta_nat_norm/= factE. +rewrite -!EFinM/onem/=. +congr _%:E; lra. + admit. + admit. + admit. + admit. + lra. Admitted. End beta_example. @@ -216,7 +242,7 @@ Proof. move=> my; rewrite letin'E/=. rewrite integral_uniform//= => _ /= Y mY /=. have /= := measurable_kernel u _ my measurableT _ mY. -move/measurable_ysection => /(_ R x) /=. +move/measurable_ysection => /(_ x) /=. set A := (X in measurable X). set B := (X in _ -> measurable X). suff : A = B by move=> ->. @@ -240,6 +266,22 @@ apply: s01. by rewrite inE in p01. Qed. +(* Lemma measurable_mtyp (t : typ) (U : set (@mtyp R t)) : measurable U. +Proof. +induction t => //. *) + +Lemma congr_letinl g t1 t2 str (e1 e2 : @exp _ _ g t1) +(e : @exp _ _ (_ :: g) t2) x U : + (forall y V, execP e1 y V = execP e2 y V) -> + measurable U -> + @execP R g t2 [let str := e1 in e] x U = + @execP R g t2 [let str := e2 in e] x U. +Proof. +move=> He mU. +apply eq_sfkernel in He. +by rewrite !execP_letin He. +Qed. + Lemma congr_letin g t1 t2 str (e : @exp _ _ _ t1) (e1 e2 : @exp _ _ (_ :: g) t2) x U : (forall y V, execP e1 (y, x) V = execP e2 (y, x) V) -> @execP R g t2 [let str := e in e1] x U = @execP R g t2 [let str := e in e2] x U. @@ -338,14 +380,17 @@ Lemma exec_beta_a1 U : Proof. rewrite execP_letin !execP_sample execD_beta_nat !execD_bernoulli_trunc/=. rewrite !execD_real/= exp_var'E (execD_var_erefl "p")/=. -transitivity (beta_nat_bern 6 4 1 0 tt U : \bar R). +transitivity (beta_nat_bern 6 4 1 0 U : \bar R). rewrite /beta_nat_bern !letin'E/= /ubeta_nat_pdf/= /onem. apply: eq_integral => x _. rewrite /=. do 2 f_equal. by rewrite /ubeta_nat_pdf' expr1 expr0 mulr1. -rewrite beta_nat_bern_bern/= /bernoulli/= bernoulli_truncE; last by lra. -rewrite measure_addE/= /mscale/=. +rewrite beta_nat_bern_bern// !bernoulli_truncE; last 2 first. + by lra. + apply/andP; split. + by apply/Baa'bb'Bab_ge0. + by apply/Baa'bb'Bab_le1. congr (_ * _ + _ * _)%:E. rewrite /Baa'bb'Bab /beta_nat_norm/=. rewrite !factS/= fact0. @@ -356,19 +401,47 @@ by field. Qed. Definition casino0 : @exp R _ [::] _ := - [Normalize - let "p" := Sample {exp_uniform 0 1 a01} in - let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in - let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in - let "a2" := Sample {exp_binomial_trunc 3 [#{"p"}]} in - return {1}:N <= #{"a2"}]. + [Normalize + let "p" := Sample {exp_uniform 0 1 a01} in + let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in + let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in + let "a2" := Sample {exp_binomial_trunc 3 [#{"p"}]} in + return {1}:N <= #{"a2"}]. Definition casino1 : @exp R _ [::] _ := - [Normalize - let "p" := Sample {exp_uniform 0 1 a01} in - let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in - let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in - Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. + [Normalize + let "p" := Sample {exp_uniform 0 1 a01} in + let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in + let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in + Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. + +Definition casino2 : @exp R _ [::] _ := + [Normalize + let "p" := Sample {exp_uniform 0 1 a01} in + let "_" := + Score {[{56}:R * #{"p"} ^+ {5%nat} * {[{1}:R - #{"p"}]} ^+ {3%nat}]} in + Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. + +Definition casino2' : @exp R _ [::] _ := + [Normalize + let "p" := Sample {exp_beta 1 1} in + let "_" := Score + {[{56}:R * #{"p"} ^+ {5%nat} * {[{1}:R - #{"p"}]} ^+ {3%nat}]} in + Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. + +Definition casino3 : @exp R _ [::] _ := + [Normalize + let "_" := Score {1 / 9}:R in + let "p" := Sample {exp_beta 6 4} in + Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. + +Definition casino4 : @exp R _ [::] _ := + [Normalize + let "_" := Score {1 / 9}:R in + Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. + +Definition casino5 : @exp R _ [::] _ := + [Normalize Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. Lemma casino01 : execD casino0 = execD casino1. @@ -391,12 +464,6 @@ move: r01 => /andP[r0 r1]. by apply/binomial_over1/andP. Qed. -Definition casino2 : @exp R _ [::] _ := - [Normalize - let "p" := Sample {exp_uniform 0 1 a01} in - let "_" := Score {[{56}:R * #{"p"} ^+ {5%nat} * {[{1}:R - #{"p"}]} ^+ {3%nat}]} in - Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. - Lemma casino12 : execD casino1 = execD casino2. Proof. @@ -431,11 +498,88 @@ apply/exprn_ge0. by rewrite subr_ge0. Qed. -Definition casino3 : @exp R _ [::] _ := - [Normalize - let "_" := Score {1 / 9}:R in - let "p" := Sample {exp_beta 6 4} in - Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. +Lemma casino22' : + execD casino2 = execD casino2'. +Proof. +apply: eq_execD. +f_equal. +apply: congr_normalize => //= x U. +apply: congr_letinl => //= y V. +rewrite !execP_sample execD_uniform execD_beta_nat/=. +rewrite beta11_uniform//. +rewrite /=. +apply: sub_sigma_algebra. +rewrite //. +admit. +Admitted. + +Lemma casino23 : + execD casino2' = execD casino3. +Proof. +apply: eq_execD. +f_equal. +apply: congr_normalize => x U. +rewrite !execP_letin !execP_sample !execP_score !execD_beta_nat. +rewrite !execD_bernoulli_trunc/= !(@execD_bin _ _ binop_mult). +rewrite !execD_pow !(@execD_bin _ _ binop_minus) !execD_real/=. +rewrite !execD_pow !(@execD_bin _ _ binop_minus) !execD_real/=. +rewrite !exp_var'E !(execD_var_erefl "p")/=. +rewrite !letin'E/= ![in RHS]ge0_integral_mscale//=. +rewrite /=. +under eq_integral => y _. + rewrite letin'E/= /mscale/=. + over. +rewrite /=. +(* set f := letin' _ _. *) +(* oops... +transitivity (\int[beta_nat 1 1]_(y in `[0%R, 1%R]) f (y, x) U)%E. + rewrite [in RHS]integral_mkcond /=. + apply: eq_integral => y _. + rewrite patchE. + case: ifPn => //. + simpl in *. + rewrite mem_setE /= in_itv /= negb_and -!ltNge => /orP[y0|y1]; + rewrite /bernoulli_trunc/=. + case: sumbool_ler. + move=> a. + by rewrite ltNge a in y0. + rewrite /prob_lang.bernoulli0 /bernoulli => _. + rewrite [LHS]measure_addE/= /mscale/=. + (* match default value *) + admit. + admit. + rewrite integral_beta_nat. + admit. +rewrite (integral_beta_nat 6 4). + rewrite ger0_norm// integral_dirac// diracT mul1e letin'E/=. + transitivity (((1 / 9)%:E * \int[beta_nat 6 4]_(y in `[0%R, 1%R]) + bernoulli_trunc (1 - (1 - y) ^+ 3) U)%E : \bar R); last first. + admit. +rewrite (integral_beta_nat 6 4)//=. +rewrite -integralZl//=. + apply: eq_integral => y y01. + rewrite /f letin'E /= !ge0_integral_mscale//= integral_dirac// diracT mul1e. + rewrite -muleAC muleC -[in RHS]muleCA. + congr (_ * _)%E. + rewrite ger0_norm. + rewrite /beta_nat_pdf ubeta_nat_pdf11/= muleC -!EFinM. + rewrite !div1r. + rewrite /beta_nat_norm/= /ubeta_nat_pdf/ubeta_nat_pdf' factE/=/onem. + congr _%:E; lra. + rewrite inE/= in_itv/= in y01. + move: y01 => /andP[y0 y1]. + apply/mulr_ge0/exprn_ge0 => //. + apply/mulr_ge0/exprn_ge0 => //. + lra. +admit. +admit. +admit. +apply: (@measurableT_comp _ _ _ _ _ _ (fun x0 => f x0 U)). + apply: measurable_kernel. + done. +apply: measurable_pair2. +admit.*) +Admitted. Lemma casino34' U : @execP R [::] _ [let "p" := Sample {exp_beta 6 4} in @@ -445,39 +589,34 @@ Proof. rewrite execP_letin !execP_sample execD_beta_nat !execD_bernoulli_trunc/=. rewrite execD_pow/= (@execD_bin _ _ binop_minus) !execD_real/=. rewrite exp_var'E (execD_var_erefl "p")/=. -transitivity (beta_nat_bern 6 4 0 3 tt U : \bar R). - rewrite /beta_nat_bern !letin'E/= /ubeta_nat_pdf/= /onem. - apply: eq_integral => x _. - do 2 f_equal. - rewrite /ubeta_nat_pdf'. - rewrite expr0. - by rewrite mul1r. -rewrite bernoulli_truncE; last by lra. -rewrite beta_nat_bern_bern/= /bernoulli/=. -rewrite measure_addE/= /mscale/=. -congr (_ * _ + _ * _)%:E; rewrite /onem. - rewrite /Baa'bb'Bab /beta_nat_norm/=. - by rewrite !factS/= fact0; field. -rewrite /Baa'bb'Bab /beta_nat_norm/=. -by rewrite !factS/= fact0; field. +(* TODO: generalize *) +rewrite letin'E/=. +have := (@beta_nat_bern_bern R 6 4 0 3 U). +rewrite /beta_nat_bern /ubeta_nat_pdf/ubeta_nat_pdf'/=. +under eq_integral do rewrite expr0 mul1r. +move=> ->//. +rewrite /Baa'bb'Bab addn0 /beta_nat_norm/= factE/=. +by congr (bernoulli_trunc _ _); field. Qed. Lemma bern_onem (f : _ -> R) U p : - (\int[beta_nat 6 4]_y bernoulli_trunc (f y) U = p%:E * \d_true U + `1-p%:E * \d_false U)%E -> - (\int[beta_nat 6 4]_y bernoulli_trunc (1 - f y) U = `1-p%:E * \d_true U + p%:E * \d_false U)%E. + (forall x, 0 <= f x <= 1) -> + (\int[beta_nat 6 4]_y bernoulli_trunc (f y) U = + p%:E * \d_true U + `1-p%:E * \d_false U)%E -> + (\int[beta_nat 6 4]_y bernoulli_trunc (1 - f y) U + = `1-p%:E * \d_true U + p%:E * \d_false U)%E. Proof. +move=> f01. under eq_integral => x _. rewrite bernoulli_truncE. over. -admit. -rewrite /= /beta_nat/mscale/= /beta_nat_norm/= /ubeta_nat/ubeta_nat_pdf. +done. +move=> h1. +rewrite /= in h1. +rewrite /bernoulli_trunc. +(* /beta_nat/mscale/= /beta_nat_norm/= /ubeta_nat/ubeta_nat_pdf/=. *) Admitted. -Definition casino4 : @exp R _ [::] _ := - [Normalize - let "_" := Score {1 / 9}:R in - Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. - Lemma casino34 : execD casino3 = execD casino4. Proof. @@ -493,21 +632,24 @@ transitivity (\int[beta_nat 6 4]_y bernoulli_trunc (1 - (1 - y) ^+ 3) U : \bar R rewrite bernoulli_truncE; last by lra. have -> := (@bern_onem (fun x => (1 - x) ^+ 3) U (1 / 11) _). congr (_ * _ + _ * _)%E; congr _%:E; rewrite /onem; lra. -transitivity (beta_nat_bern 6 4 0 3 tt U : \bar R). - rewrite /beta_nat_bern !letin'E/= /ubeta_nat_pdf/= /onem. + admit. +transitivity (beta_nat_bern 6 4 0 3 U : \bar R). + rewrite /beta_nat_bern /ubeta_nat_pdf/= /onem. apply: eq_integral => y0 _. do 2 f_equal. rewrite /ubeta_nat_pdf'/=. rewrite expr0. by rewrite mul1r. -rewrite beta_nat_bern_bern/= /bernoulli/=. -rewrite measure_addE/= /mscale/=. +rewrite beta_nat_bern_bern//= bernoulli_truncE; last first. + apply/andP; split. + apply/Baa'bb'Bab_ge0. + apply/Baa'bb'Bab_le1. congr (_ * _ + _ * _)%:E; rewrite /onem. rewrite /Baa'bb'Bab /beta_nat_norm/=. by rewrite !factS/= fact0; field. rewrite /Baa'bb'Bab /beta_nat_norm/=. by rewrite !factS/= fact0; field. -Qed. +Admitted. Lemma norm_score_bern g p1 p2 (p10 : p1 != 0) (p1_ge0 : 0 <= p1) (p201 : 0 <= p2 <= 1) : @@ -544,9 +686,6 @@ rewrite integral_dirac//= diracT !diracE. by rewrite muleC muleA -EFinM mulVf// invr1 /onem !(mul1r, mule1). Qed. -Definition casino5 : @exp R _ [::] _ := - [Normalize Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. - Lemma casino45 : execD casino4 = execD casino5. Proof. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 42fd07c6a..1015136a0 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -6,14 +6,21 @@ From mathcomp Require Import mathcomp_extra boolp classical_sets. From mathcomp Require Import functions cardinality fsbigop. From mathcomp Require Import reals ereal signed topology normedtype sequences. From mathcomp Require Import esum measure lebesgue_measure numfun. -From mathcomp Require Import lebesgue_integral exp kernel. +From mathcomp Require Import lebesgue_integral exp kernel charge. From mathcomp Require Import ring lra. -(******************************************************************************) -(* Semantics of a probabilistic programming language using s-finite kernels *) -(* *) -(* bernoulli r1 == Bernoulli probability with r1 a proof that *) -(* r : {nonneg R} is smaller than 1 *) +(**md**************************************************************************) +(* # Semantics of a probabilistic programming language using s-finite kernels *) +(* bernoulli p1 == Bernoulli probability with p1 a proof that *) +(* p : {nonneg R} is smaller than 1 *) +(* bernoulli_trunc r == Bernoulli probability with real number r *) +(* bino_term n k p == $\binom{n}{k}p^k (1-p)^(n-k)$ *) +(* Computes a binomial distribution term for *) +(* k successes in n trials with success rate p *) +(* binomial_probability n p1 == binomial probability with n and p1 a proof *) +(* that p : {nonneg R} is smaller than 1 *) +(* binomial_probability_trunc n r == binomial probability with n and real *) +(* number r *) (* uniform_probability a b ab0 == uniform probability over the interval [a,b] *) (* sample mP == sample according to the probability P where mP is a *) (* proof that P is a measurable function *) @@ -195,7 +202,21 @@ Definition bernoulli_trunc (p : R) := match sumbool_ler 0%R p with | inr _ => bernoulli0 end. -(*HB.instance Definition _ (p : R) := Probability.on (bernoulli_trunc p).*) +Lemma bernoulli_truncE (p : R) U : + (0 <= p <= 1)%R -> + (bernoulli_trunc p U = + p%:E * \d_true U + (`1-p)%:E * \d_false U)%E. +Proof. +move=> /andP[p0 p1]. +rewrite /bernoulli_trunc. +case: (sumbool_ler 0 p) => [{}p0/=|]. + case: (sumbool_ler p 1) => [{}p1/=|]. + by rewrite /bernoulli/= measure_addE. + by rewrite ltNge p1. +by rewrite ltNge p0. +Qed. + +(* HB.instance Definition _ (p : R) := Probability.on (bernoulli_trunc p). *) Let simpe := (@mule0 R, @adde0 R, @mule1 R, @add0e R). @@ -225,7 +246,7 @@ have [->|->|->|->] /= := subset_set2 UT. (fun x => if 0 <= x <= 1 then (`1-x)%:E else 1%E))//. apply: measurable_fun_ifT => //=. by apply: measurable_and => //; exact: measurable_fun_ler. - by apply/EFin_measurable_fun; apply/measurable_funB. + by apply/measurable_EFinP; apply/measurable_funB. apply/funext => x/=; case: sumbool_ler => /= x0. case: sumbool_ler => /= x1. rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. @@ -421,16 +442,16 @@ rewrite (_ : f = fun x => if 0 <= x <= 1 then (\sum_(m < n.+1) else \d_0%N Ys)//. move=> ?; apply: measurable_fun_ifT => //=. by apply: measurable_and => //; exact: measurable_fun_ler. - apply: emeasurable_fun_sum => m /=. + apply: emeasurable_sum => m /=. rewrite /mscale/= [X in measurable_fun _ X](_ : _ = (fun x => ((x ^+ m * `1-x ^+ (n - m) *+ 'C(n, m))%:E * \d_(nat_of_ord m) Ys)%E)); last first. by apply:funext => x; case: sumbool_ler => // x0; case: sumbool_ler. - apply: emeasurable_funM => //; apply/EFin_measurable_fun => //. + apply: emeasurable_funM => //; apply/measurable_EFinP => //. under eq_fun do rewrite -mulrnAr. apply: measurable_funM => //. under eq_fun do rewrite -mulr_natr. apply: measurable_funM => //=. - apply: measurable_fun_pow. + apply: measurable_funX. exact: measurable_funB. rewrite {}/f. apply/funext => x. @@ -534,6 +555,8 @@ move=> mE m. by rewrite !integral_indic//= /uniform_probability/= /mscale/= /mrestr setIT. Qed. +Import HBNNSimple. + Let integral_uniform_nnsfun (f : {nnsfun _ >-> R}) (a b : R) (ab0 : (0 < b - a)%R) : let m := uniform_probability ab0 in \int[m]_x (f x)%:E = @@ -542,10 +565,10 @@ Proof. move=> m. under [LHS]eq_integral do rewrite fimfunE -fsumEFin//. rewrite [LHS]ge0_integral_fsum//; last 2 first. - - by move=> r; exact/EFin_measurable_fun/measurableT_comp. + - by move=> r; exact/measurable_EFinP/measurableT_comp. - by move=> n x _; rewrite EFinM nnfun_muleindic_ge0. rewrite -[RHS]ge0_integralZl//; last 3 first. - - exact/EFin_measurable_fun/measurable_funTS. + - exact/measurable_EFinP/measurable_funTS. - by move=> x _; rewrite lee_fin. - by rewrite lee_fin invr_ge0// ltW. under [RHS]eq_integral. @@ -553,12 +576,12 @@ under [RHS]eq_integral. by move=> r; rewrite EFinM nnfun_muleindic_ge0. over. rewrite [RHS]ge0_integral_fsum//; last 2 first. - - by move=> r; apply/EFin_measurable_fun; do 2 apply/measurableT_comp => //. + - by move=> r; apply/measurable_EFinP; do 2 apply/measurableT_comp => //. - move=> n x _; rewrite EFinM mule_ge0//; last by rewrite nnfun_muleindic_ge0. by rewrite lee_fin invr_ge0// ltW. apply: eq_fsbigr => r _; rewrite ge0_integralZl//. - by rewrite !integralZl_indic_nnsfun//= integral_uniform_indic// muleCA. -- exact/EFin_measurable_fun/measurableT_comp. +- exact/measurable_EFinP/measurableT_comp. - by move=> t _; rewrite nnfun_muleindic_ge0. - by rewrite lee_fin invr_ge0// ltW. Qed. @@ -570,30 +593,30 @@ Lemma integral_uniform (f : _ -> \bar R) (a b : R) (b - a)^-1%:E * \int[lebesgue_measure]_(x in `[a, b]) f x. Proof. move=> mf f0 m. -have [f_ [ndf_ f_f]] := approximation measurableT mf (fun x0 _ => f0 x0). +pose f_ := nnsfun_approx measurableT mf. transitivity (lim (\int[m]_(x in setT) (f_ n x)%:E @[n --> \oo])). rewrite -monotone_convergence//=. - apply: eq_integral => ? /[!inE] xD; apply/esym/cvg_lim => //=. - exact: f_f. - - by move=> n; exact/EFin_measurable_fun/measurable_funTS. + exact/cvg_nnsfun_approx. + - by move=> n; exact/measurable_EFinP/measurable_funTS. - by move=> n ? _; rewrite lee_fin. - - by move=> ? _ ? ? mn; rewrite lee_fin; exact/lefP/ndf_. + - by move=> ? _ ? ? mn; rewrite lee_fin; exact/lefP/nd_nnsfun_approx. rewrite [X in _ = _ * X](_ : _ = lim (\int[lebesgue_measure]_(x in `[a, b]) (f_ n x)%:E @[n --> \oo])); last first. rewrite -monotone_convergence//=. - - by apply: eq_integral => ? /[!inE] xD; apply/esym/cvg_lim => //; exact: f_f. - - by move=> n; exact/EFin_measurable_fun/measurable_funTS. + - by apply: eq_integral => ? /[!inE] xD; apply/esym/cvg_lim => //; exact: cvg_nnsfun_approx. + - by move=> n; exact/measurable_EFinP/measurable_funTS. - by move=> n ? _; rewrite lee_fin. - - by move=> ? _ ? ? /ndf_ /lefP; rewrite lee_fin. + - by move=> ? _ ? ? ab; rewrite lee_fin; exact/lefP/nd_nnsfun_approx. rewrite -limeMl//. apply: congr_lim. by apply/funext => n /=; exact: integral_uniform_nnsfun. apply/ereal_nondecreasing_is_cvgn => ? ? ab; apply: ge0_le_integral => //=. - by move=> ? _; rewrite lee_fin. -- exact/EFin_measurable_fun/measurable_funTS. +- exact/measurable_EFinP/measurable_funTS. - by move=> ? _; rewrite lee_fin. -- exact/EFin_measurable_fun/measurable_funTS. -- by move=> ? _; rewrite lee_fin; move/ndf_ : ab => /lefP. +- exact/measurable_EFinP/measurable_funTS. +- by move=> ? _; rewrite lee_fin; exact/lefP/nd_nnsfun_approx. Qed. End integral_uniform. @@ -645,7 +668,7 @@ Qed. Lemma measurable_ubeta_nat_pdf : measurable_fun setT ubeta_nat_pdf. Proof. -by apply /measurable_funM => //; exact/measurable_fun_pow/measurable_funB. +by apply /measurable_funM => //; exact/measurable_funX/measurable_funB. Qed. Lemma beta_nat_normE : @@ -676,6 +699,15 @@ Definition ubeta_nat (U : set (measurableTypeR R)) : \bar R := \int[mu]_(x in U `&` `[0, 1](*NB: is this correct?*)) (ubeta_nat_pdf x)%:E. (* TODO: define as \int[uniform_probability p01]_(t in U) (ubeta_nat_pdf t)%:E ? *) +Lemma ubeta_natE U : + (ubeta_nat U = + \int[mu]_(x in U `&` `[0%R, 1%R]) (ubeta_nat_pdf x)%:E :> \bar R)%E. +Proof. by []. Qed. + +Lemma ubeta_nat_lty U : (ubeta_nat U < +oo)%E. +Proof. +Admitted. + Let ubeta_nat0 : ubeta_nat set0 = 0%:E. Proof. by rewrite /ubeta_nat set0I integral_set0. Qed. @@ -685,7 +717,6 @@ rewrite /ubeta_nat integral_ge0//= => x [Ux]. by rewrite in_itv/= => x01; rewrite lee_fin ubeta_nat_pdf_ge0. Qed. -(* TODO: should be shorter *) Let ubeta_nat_sigma_additive : semi_sigma_additive ubeta_nat. Proof. move=> /= F mF tF mUF; rewrite /ubeta_nat setI_bigcupl; apply: cvg_toP. @@ -695,20 +726,7 @@ move=> /= F mF tF mUF; rewrite /ubeta_nat setI_bigcupl; apply: cvg_toP. by rewrite lee_fin; exact: ubeta_nat_pdf_ge0. rewrite ge0_integral_bigcup//=. - by move=> k; exact: measurableI. -- apply/integrableP; split. - by apply/EFin_measurable_fun; exact: measurable_funTS measurable_ubeta_nat_pdf. - apply: le_lt_trans => /=. - apply: (@subset_integral _ _ _ mu _ `[0%R, 1%R]) => //=. - - rewrite -setI_bigcupl; apply: measurableI => //. - - apply/measurableT_comp => //; apply/measurableT_comp => //. - exact: measurable_funTS measurable_ubeta_nat_pdf. - - by apply: bigcup_sub => k _; exact: subIsetr. - rewrite /=. - under eq_integral. - move=> /= x; rewrite inE/= in_itv/= => x01. - rewrite ger0_norm//; last by rewrite ubeta_nat_pdf_ge0. - over. - by rewrite -beta_nat_normE ltry. +- by apply/measurable_EFinP; exact: measurable_funTS measurable_ubeta_nat_pdf. - move=> x [k _ [_]]; rewrite /= in_itv/= => x01. by rewrite lee_fin ubeta_nat_pdf_ge0. - exact: trivIset_setIr. @@ -733,19 +751,6 @@ Proof. move=> /= F mF tF mUF; exact: measure_semi_sigma_additive. Qed. HB.instance Definition _ := isMeasure.Build _ _ _ beta_nat beta_nat0 beta_nat_ge0 beta_nat_sigma_additive. -(* HB.instance Definition _ := Measure.on beta_nat. *) - -(*Let beta_nat0 : beta_nat set0 = 0. -Proof. exact: measure0. Qed. - -Let beta_nat_ge0 U : (0 <= beta_nat U)%E. -Proof. exact: measure_ge0. Qed. - -Let beta_nat_sigma_additive : semi_sigma_additive beta_nat. -Proof. move=> /= F mF tF mUF; exact: measure_semi_sigma_additive. Qed. - -HB.instance Definition _ := isMeasure.Build _ _ _ beta_nat beta_nat0 beta_nat_ge0 beta_nat_sigma_additive.*) - Let beta_nat_setT : beta_nat setT = 1%:E. Proof. rewrite /beta_nat /= /mscale /=. @@ -765,6 +770,8 @@ Qed. End beta_probability. +Arguments beta_nat {R}. + Section beta_probability11. Local Open Scope ring_scope. Context {R : realType}. @@ -786,6 +793,356 @@ Qed. End beta_probability11. +Lemma factD n m : (n`! * m`! <= (n + m).+1`!)%N. +Proof. +elim: n m => /= [m|n ih m]. + by rewrite fact0 mul1n add0n factS leq_pmull. +rewrite 2!factS [in X in (_ <= _ * X)%N]addSn -mulnA leq_mul//. +by rewrite ltnS addSnnS leq_addr. +Qed. + +Lemma factD' n m : (n`! * m.-1`! <= (n + m)`!)%N. +Proof. +case: m => //= [|m]. + by rewrite fact0 muln1 addn0. +by rewrite addnS factD. +Qed. + +Lemma leq_prod2 (x y n m : nat) : (n <= x)%N -> (m <= y)%N -> + (\prod_(m <= i < y) i * \prod_(n <= i < x) i <= \prod_(n + m <= i < x + y) i)%N. +Proof. +move=> nx my. +rewrite big_addn. +rewrite -addnBA//. +rewrite {3}/index_iota. +rewrite -addnBAC//. +rewrite iotaD. +rewrite big_cat/=. +rewrite mulnC. +rewrite leq_mul//. + rewrite /index_iota. + apply: leq_prod. + by move=> i _; rewrite leq_addr. +rewrite subnKC//. +rewrite -{1}(add0n m). +rewrite big_addn. +rewrite {2}(_ : (y - m) = ((y - m + x) - x))%N; last first. + by rewrite -addnBA// subnn addn0. +rewrite -{1}(add0n x). +rewrite big_addn. +rewrite -addnBA// subnn addn0. +apply: leq_prod => i _. +by rewrite leq_add2r leq_addr. +Qed. + +Lemma leq_fact2 (x y n m : nat) : + (n <= x) %N -> (m <= y)%N -> + (x`! * y`! * ((n + m).+1)`! <= n`! * m`! * ((x + y).+1)`!)%N. +Proof. +move=> nx my. +rewrite (_ : x`! = n`! * \prod_(n.+1 <= i < x.+1) i)%N; last first. + by rewrite -fact_split. +rewrite -!mulnA leq_mul2l; apply/orP; right. +rewrite (_ : y`! = m`! * \prod_(m.+1 <= i < y.+1) i)%N; last first. + by rewrite -fact_split. +rewrite mulnCA -!mulnA leq_mul2l; apply/orP; right. +rewrite (_ : (x + y).+1`! = (n + m).+1`! * \prod_((n + m).+2 <= i < (x + y).+2) i)%N; last first. + rewrite -fact_split//. + by rewrite ltnS leq_add. +rewrite mulnA mulnC leq_mul2l; apply/orP; right. +rewrite -addSn -addnS. +rewrite -addSn -addnS. +exact: leq_prod2. +Qed. + +Section integral_beta. +Context {R : realType}. +Variables a b a' b' : nat. + +Local Notation mu := lebesgue_measure. + +Lemma integralMl f g1 g2 A : +measurable A -> measurable_fun A f -> + measurable_fun A g1 -> measurable_fun A g2 -> +(ae_eq mu A g1 (EFin \o g2)) -> + \int[mu]_(x in A) (f x * g1 x) = + \int[mu]_(x in A) (f x * (g2 x)%:E) :> \bar R. +Proof. +move=> mA mf mg1 mg2 Hg. +apply: ae_eq_integral => //. + by apply: emeasurable_funM. + apply: emeasurable_funM => //. + by apply/measurable_EFinP. +by apply: ae_eq_mul2l. +Qed. + +Let beta_nat_dom : (@beta_nat R a b `<< mu). +Proof. +move=> A mA muA0. +rewrite /beta_nat /mscale/= /ubeta_nat. +have -> : \int[mu]_(x0 in A `&` `[0%R, 1%R]) (ubeta_nat_pdf a b x0)%:E = 0%:E. + apply/eqP; rewrite eq_le. + apply/andP; split; last first. + apply: integral_ge0 => x [Ax /=]. + rewrite in_itv /= => x01. + by rewrite lee_fin ubeta_nat_pdf_ge0. + apply: le_trans. + apply: (@ge0_subset_integral _ _ _ _ _ A). + by apply: measurableI. + by []. + apply/measurable_EFinP. + apply: (@measurable_funS _ _ _ _ setT) => //=. + apply: measurable_ubeta_nat_pdf. + move=> x Ax. + have : (`[0%R, 1%R]%classic x). + admit. + rewrite /= in_itv/=. + apply: ubeta_nat_pdf_ge0. + apply: subIsetl. + rewrite /=. + (* rewrite integral_abs_eq0. *) (* without abs *) + admit. +by rewrite mule0. +Admitted. + +Lemma integral_beta_nat f : + measurable_fun setT f -> + \int[beta_nat a b]_(x in `[0%R, 1%R]) `|f x| < +oo -> + \int[beta_nat a b]_(x in `[0%R, 1%R]) f x = + \int[mu]_(x in `[0%R, 1%R]) (f x * (beta_nat_pdf a b x)%:E) :> \bar R. +Proof. +move=> mf finf. +rewrite -(Radon_Nikodym_change_of_variables beta_nat_dom) //=. +apply: integralMl => //. + apply: (@measurable_funS _ _ _ _ [set: R]) => //. + apply: (@measurable_funS _ _ _ _ [set: R]) => //. + rewrite Radon_NikodymE. + by exact: beta_nat_dom. + move=> /= H. + case: cid => /= h [h1 h2 h3]. + have : (measurable_fun setT h /\ \int[mu]_x `|h x| < +oo). + apply/integrableP/h2. + move=> /= [mh _]. + apply: mh. + apply: (@measurable_funS _ _ _ _ [set: R]) => //. + apply: measurable_beta_nat_pdf. + rewrite Radon_NikodymE => /= A. + by exact: beta_nat_dom. +case: cid => /= h [h1 h2 h3]. +apply: integral_ae_eq => //. + apply: integrableS h2 => //. (* integrableST? *) + apply: (@measurable_funS _ _ _ _ [set: R]) => //. + apply: measurableT_comp => //. + apply: measurable_beta_nat_pdf. + move=> E E01 mE. + have mB : measurable_fun E (EFin \o ubeta_nat_pdf a b). + apply: measurableT_comp => //. + apply: (@measurable_funS _ _ _ _ [set: R]) => //. + apply: measurable_ubeta_nat_pdf. + rewrite -(h3 _ mE). + rewrite /beta_nat/mscale/ubeta_nat/beta_nat_pdf/=. + under eq_integral do rewrite mulrC EFinM. + rewrite (integralZl mE). + rewrite /ubeta_nat setIidl //. + rewrite /=. + apply/integrableP; split. + by apply: mB. + under eq_integral => x x01. + rewrite gee0_abs /=. + over. + apply: ubeta_nat_pdf_ge0. + have : x \in `[0%R, 1%R]. + apply: (@subset_trans _ _ `[x,x] _ _ E01). + by rewrite set_interval.set_itv1 sub1set x01. + by rewrite /= in_itv/= lexx. + by rewrite in_itv/=. + rewrite /=. + have <- := (setIidl E01). + by rewrite -ubeta_natE ubeta_nat_lty. +apply/integrableP; split. + by apply: (@measurable_funS _ _ _ _ [set: R]). +exact: finf. +Qed. + +Local Open Scope ring_scope. + +(* TODO: `[0, 1]? *) +Definition beta_nat_bern U : \bar R := + \int[beta_nat a b]_y bernoulli_trunc (ubeta_nat_pdf a'.+1 b'.+1 y) U. + +Local Notation B := beta_nat_norm. + +Definition Baa'bb'Bab : R := (beta_nat_norm (a + a') (b + b')) / beta_nat_norm a b. + +Lemma Baa'bb'Bab_ge0 : 0 <= Baa'bb'Bab. +Proof. by rewrite /Baa'bb'Bab divr_ge0// beta_nat_norm_ge0. Qed. + +Definition Baa'bb'Bab_nneg : {nonneg R} := NngNum Baa'bb'Bab_ge0. + +Lemma Baa'bb'Bab_le1 : Baa'bb'Bab_nneg%:num <= 1. +Proof. +rewrite /Baa'bb'Bab_nneg/= /Baa'bb'Bab. +rewrite ler_pdivrMr// ?mul1r ?beta_nat_norm_gt0//. +rewrite /B /beta_nat_norm. +rewrite ler_pdivrMr ?ltr0n ?fact_gt0//. +rewrite mulrAC. +rewrite ler_pdivlMr ?ltr0n ?fact_gt0//. +rewrite -!natrM ler_nat. +case: a. + rewrite /= fact0 mul1n !add0n. + case: b => /=. + case: a' => //. + case: b' => //= m. + by rewrite fact0 !mul1n muln1. + move=> n/=. + by rewrite fact0 add0n muln1 mul1n factD'. + move=> m. + rewrite mulnC leq_mul// mulnC. + by rewrite (leq_trans (factD' _ _))// addSn addnS//= addnC. +move=> n. +rewrite addSn. +case: b. + rewrite !fact0 add0n muln1 [leqRHS]mulnC addn0/= leq_mul//. + by rewrite factD'. +move=> m. +clear a b. +rewrite [(n + a').+1.-1]/=. +rewrite [n.+1.-1]/=. +rewrite [m.+1.-1]/=. +rewrite addnS. +rewrite [(_ + m).+1.-1]/=. +rewrite (addSn m b'). +rewrite [(m + _).+1.-1]/=. +rewrite (addSn (n + a')). +rewrite [_.+1.-1]/=. +rewrite addSn addnS. +by rewrite leq_fact2// leq_addr. +Qed. + +Lemma onem_Baa'bb'Bab_ge0 : 0 <= 1 - (Baa'bb'Bab_nneg%:num). +Proof. by rewrite subr_ge0 Baa'bb'Bab_le1. Qed. + +Lemma onem_Baa'bb'Bab_ge0_fix : 0 <= B a b * (1 - Baa'bb'Bab_nneg%:num). +Proof. +rewrite mulr_ge0//. + rewrite /B. + exact: beta_nat_norm_ge0. +rewrite subr_ge0. +exact: Baa'bb'Bab_le1. +Qed. + +Lemma ubeta_nat_pdf_ge0' t : 0 <= ubeta_nat_pdf a'.+1 b'.+1 t :> R. +Proof. +apply: ubeta_nat_pdf_ge0. (* TODO: needs 0 <= t <= 1 *) +Admitted. + +Lemma ubeta_nat_pdf_le1' t : (NngNum (ubeta_nat_pdf_ge0' t))%:num <= 1 :> R. +Proof. +rewrite /=. +rewrite /ubeta_nat_pdf. +rewrite /ubeta_nat_pdf'. (* TODO: needs 0 <= t <= 1 *) +Admitted. + +Lemma integral_ubeta_nat' : + (\int[ubeta_nat a b]_x (ubeta_nat_pdf a'.+1 b'.+1 x)%:E = + \int[mu]_(x in `[0%R, 1%R]) + (x ^+ a'.-1 * `1-x ^+ b'.-1 * x ^+ a * `1-x ^+ b)%:E :> \bar R)%E. +Proof. +rewrite /ubeta_nat/ubeta_nat_pdf. +Admitted. + +Lemma beta_nat_bern_bern U : + (a > 0)%N -> (b > 0)%N -> + beta_nat_bern U = + bernoulli_trunc Baa'bb'Bab U. +Proof. +rewrite /beta_nat_bern. +transitivity ((\int[beta_nat a b]_(y in `[0%R, 1%R]) + bernoulli_trunc (ubeta_nat_pdf a'.+1 b'.+1 y) U)%E : \bar R). + admit. +rewrite integral_beta_nat /=; last 2 first. + apply: (@measurableT_comp _ _ _ _ _ _ (bernoulli_trunc ^~ U)). + apply: (measurability (ErealGenInftyO.measurableE R)) => //=. + move=> /= _ [_ [x ->] <-]; apply: measurableI => //. + admit. + exact: measurable_ubeta_nat_pdf. + admit. +under eq_integral => x. + rewrite inE/= in_itv/= => x01. + rewrite bernoulli_truncE. + over. + apply/andP; split. + apply/ubeta_nat_pdf_ge0/x01. + apply/ubeta_nat_pdf_le1/x01. +rewrite /=. +rewrite bernoulli_truncE; last first. + apply/andP; split. + exact: Baa'bb'Bab_ge0. + exact: Baa'bb'Bab_le1. +under eq_integral => x _. + rewrite muleC muleDr//. + over. +rewrite integralD//=; last 2 first. + (* TODO: integrableM *) + admit. + admit. +congr (_ + _). + under eq_integral do rewrite muleA muleC. + rewrite integralZl//=; last first. + admit. + rewrite muleC. + congr (_ * _)%E. + rewrite /beta_nat_pdf. + under eq_integral do rewrite EFinM -muleA muleC -muleA. + rewrite integralZl//=; last first. + admit. + transitivity (((beta_nat_norm a b)^-1)%:E * \int[lebesgue_measure]_(x in `[0%R, 1%R]) ((ubeta_nat_pdf (a+a') (b+b') x)%:E) : \bar R)%E. + congr (_ * _)%E. + apply: eq_integral => x x01. + rewrite /ubeta_nat_pdf /ubeta_nat_pdf' muleC /onem -EFinM/=. + rewrite mulrCA -mulrA -exprD mulrA -exprD. + congr (_ ^+ _ * _ ^+ _)%:E. + rewrite -!subn1 subDnCA//. + rewrite addnC -!subn1 subDnCA//. + rewrite -beta_nat_normE. + rewrite /Baa'bb'Bab/B -!EFinM. + congr _%:E. + rewrite mulrC//. +under eq_integral do rewrite muleA muleC. +rewrite integralZl//=; last first. + admit. +rewrite muleC. +congr (_ * _)%E. +rewrite /beta_nat_pdf. +under eq_integral do rewrite EFinM -muleA muleC -muleA. +rewrite integralZl//=; last first. + admit. +transitivity (((beta_nat_norm a b)^-1)%:E * \int[lebesgue_measure]_(x in `[0%R, 1%R]) ((ubeta_nat_pdf a b x)%:E - (ubeta_nat_pdf (a+a') (b+b') x)%:E) : \bar R)%E. + congr (_ * _)%E. + apply: eq_integral => x x01. + rewrite /onem -EFinM mulrBl mul1r EFinB. + congr (_ - _)%E. + rewrite /ubeta_nat_pdf /ubeta_nat_pdf'/=. + rewrite mulrCA -mulrA -exprD mulrA -exprD. + congr (_ ^+ _ * _ ^+ _)%:E. + rewrite addnC -!subn1 subDnCA//. + rewrite -!subn1 subDnCA//. +rewrite integralB_EFin//=; last 2 first. + admit. + admit. +rewrite -!beta_nat_normE -EFinM mulrBr /onem mulVf; last first. + rewrite /B mulf_eq0 negb_or. + apply/andP; split. + rewrite mulf_eq0 negb_or. + rewrite gt_eqF ?ltr0n ?fact_gt0//=. + rewrite gt_eqF ?ltr0n ?fact_gt0//=. + rewrite invr_eq0 gt_eqF ?ltr0n ?fact_gt0//=. +congr (_ - _)%:E. +by rewrite mulrC. +Admitted. + +End integral_beta. + Section mscore. Context d (T : measurableType d) (R : realType). Variable f : T -> R. From eafb5658ef5b7336503da3520b46828c94485179 Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Sun, 24 Mar 2024 21:57:17 +0900 Subject: [PATCH 26/36] complete the casino example - new version of Bernoulli - beta_nat_norm_sym - generic change variables - beta_nat_normE - cleaning --- _CoqProject | 2 +- coq-mathcomp-analysis.opam | 4 +- theories/Make | 2 +- theories/ftc.v | 58 +- theories/lang_syntax.v | 1625 ++++++++++++++--- theories/lang_syntax_examples.v | 612 ++++--- theories/lang_syntax_examples_wip.v | 696 -------- theories/lang_syntax_table_game.v | 717 ++++++++ theories/lebesgue_integral.v | 12 + theories/lebesgue_measure.v | 4 +- theories/measure.v | 4 +- theories/prob_lang.v | 2584 ++++++++++----------------- theories/prob_lang_wip.v | 216 ++- 13 files changed, 3574 insertions(+), 2962 deletions(-) delete mode 100644 theories/lang_syntax_examples_wip.v create mode 100644 theories/lang_syntax_table_game.v diff --git a/_CoqProject b/_CoqProject index 419dce077..9cb7d4f39 100644 --- a/_CoqProject +++ b/_CoqProject @@ -99,4 +99,4 @@ theories/lang_syntax_util.v theories/lang_syntax_toy.v theories/lang_syntax.v theories/lang_syntax_examples.v -theories/lang_syntax_examples_wip.v +theories/lang_syntax_table_game.v diff --git a/coq-mathcomp-analysis.opam b/coq-mathcomp-analysis.opam index c7f50a6ee..ed4e7d518 100644 --- a/coq-mathcomp-analysis.opam +++ b/coq-mathcomp-analysis.opam @@ -19,8 +19,8 @@ depends: [ "coq-mathcomp-solvable" "coq-mathcomp-field" "coq-mathcomp-bigenough" { (>= "1.0.0") } - "coq-mathcomp-algebra-tactics" { (>= "1.2.0") } - "coq-mathcomp-zify" { (>= "1.4.0") } + "coq-mathcomp-algebra-tactics" { (>= "1.2.3") } + "coq-mathcomp-zify" { (>= "1.5.0") } ] tags: [ diff --git a/theories/Make b/theories/Make index e3e65bc74..afcc341de 100644 --- a/theories/Make +++ b/theories/Make @@ -65,4 +65,4 @@ lang_syntax_util.v lang_syntax_toy.v lang_syntax.v lang_syntax_examples.v -lang_syntax_examples_wip.v +lang_syntax_table_game.v diff --git a/theories/ftc.v b/theories/ftc.v index bbc908701..4e2cab72c 100644 --- a/theories/ftc.v +++ b/theories/ftc.v @@ -29,12 +29,69 @@ Import numFieldNormedType.Exports. Local Open Scope classical_set_scope. Local Open Scope ring_scope. +(* TODO: move to normedtype.v? *) +Lemma nbhs_right_lt_lt {R : realType} (x y : R) : + (y < x)%R -> \forall z \near nbhs y^'+, (z < x)%R. +Proof. +move=> yx. +exists (x - y)%R => /=; first by rewrite subr_gt0. +move=> z/= /[swap] yz. +by rewrite ltr0_norm ?subr_lt0// opprB ltrBlDl addrC subrK. +Qed. + +(* TODO: move to normedtype.v? *) +Lemma nbhs_right_lt_le {R : realType} (x y : R) : + (y < x)%R -> \forall z \near nbhs y^'+, (z <= x)%R. +Proof. +by move=> yx; near=> z; apply/ltW; near: z; exact: nbhs_right_lt_lt. +Unshelve. all: by end_near. Qed. + +(* TODO: move to normedtype.v? *) +Lemma cvg_patch {R : realType} (f : R -> R^o) (a b : R) (x : R) : (a < b)%R -> + x \in `]a, b[ -> + f @ (x : subspace `[a, b]) --> f x -> + (f \_ `[a, b] x) @[x --> x] --> f x. +Proof. +move=> ab xab xf; apply/cvgrPdist_lt => /= e e0. +move/cvgrPdist_lt : xf => /(_ e e0) xf. +rewrite near_simpl; near=> z. +rewrite patchE ifT//; last first. + rewrite inE; apply: subset_itv_oo_cc. + by near: z; exact: near_in_itv. +near: z. +rewrite /prop_near1 /nbhs/= /nbhs_subspace ifT// in xf; last first. + by rewrite inE/=; exact: subset_itv_oo_cc xab. +case: xf => x0 /= x00 xf. +near=> z. +apply: xf => //=. +rewrite inE; apply: subset_itv_oo_cc. +by near: z; exact: near_in_itv. +Unshelve. all: by end_near. Qed. + Section FTC. Context {R : realType}. Notation mu := (@lebesgue_measure R). Local Open Scope ereal_scope. Implicit Types (f : R -> R) (a : itv_bound R). +Let integrable_locally f (A : set R) : measurable A -> + mu.-integrable A (EFin \o f) -> locally_integrable [set: R] (f \_ A). +Proof. +move=> mA intf; split. +- move/integrableP : intf => [mf _]. + by apply/(measurable_restrictT _ _).1 => //; exact/EFin_measurable_fun. +- exact: openT. +- move=> K _ cK. + move/integrableP : intf => [mf]. + rewrite integral_mkcond/=. + under eq_integral do rewrite restrict_EFin restrict_normr. + apply: le_lt_trans. + apply: ge0_subset_integral => //=; first exact: compact_measurable. + apply/EFin_measurable_fun/measurableT_comp/EFin_measurable_fun => //=. + move/(measurable_restrictT _ _).1 : mf => /=. + by rewrite restrict_EFin; exact. +Qed. + Let FTC0 f a : mu.-integrable setT (EFin \o f) -> let F x := (\int[mu]_(t in [set` Interval a (BRight x)]) f t)%R in forall x, a < BRight x -> lebesgue_pt f x -> @@ -679,7 +736,6 @@ Qed. End Rintegration_by_parts. -(* TODO: move to realfun.v? *) Section integration_by_substitution_preliminaries. Context {R : realType}. Notation mu := lebesgue_measure. diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index 27ef25966..5a1af280e 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -4,13 +4,22 @@ From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. From mathcomp Require Import mathcomp_extra boolp classical_sets. From mathcomp Require Import functions cardinality fsbigop. From mathcomp Require Import signed reals ereal topology normedtype sequences. -From mathcomp Require Import esum measure lebesgue_measure numfun. -From mathcomp Require Import lebesgue_integral kernel prob_lang. -From mathcomp Require Import lang_syntax_util. -From mathcomp Require Import ring lra. +From mathcomp Require Import esum measure lebesgue_measure numfun derive realfun. +From mathcomp Require Import lebesgue_integral probability ftc kernel charge. +From mathcomp Require Import prob_lang lang_syntax_util. +From mathcomp Require Import lra. -(******************************************************************************) -(* Syntax and Evaluation for a Probabilistic Programming Language *) +(**md**************************************************************************) +(* # Syntax and Evaluation for a Probabilistic Programming Language *) +(* *) +(* Reference: *) +(* - R. Saito, R. Affeldt. Experimenting with an Intrinsically-Typed *) +(* Probabilistic Programming Language in Coq using s-finite kernels in Coq. *) +(* APLAS 2023 *) +(* *) +(* beta distribution specialized to nat *) +(* beta_pdf == probability density function for beta *) +(* beta_prob == beta probability measure *) (* *) (* typ == syntax for types of data structures *) (* measurable_of_typ t == the measurable type corresponding to type t *) @@ -38,9 +47,6 @@ From mathcomp Require Import ring lra. (* function from mctx g to mtyp t *) (* pval R g t == "probabilistic value", i.e., *) (* s-finite kernel, from mctx g to mtyp t *) -(* mkswap k == given a kernel k : (Y * X) ~> Z, *) -(* returns a kernel of type (X * Y) ~> Z *) -(* letin' := mkcomp \o mkswap *) (* e -D> f ; mf == the evaluation of the deterministic expression e *) (* leads to the deterministic value f *) (* (mf is the proof that f is measurable) *) @@ -61,8 +67,6 @@ Unset Printing Implicit Defensive. Import Order.TTheory GRing.Theory Num.Def Num.Theory. Import numFieldTopology.Exports. -Reserved Notation "f .; g" (at level 60, right associativity, - format "f .; '/ ' g"). Reserved Notation "e -D> f ; mf" (at level 40). Reserved Notation "e -P> k" (at level 40). @@ -70,193 +74,1297 @@ Local Open Scope classical_set_scope. Local Open Scope ring_scope. Local Open Scope ereal_scope. -(* TODO: mv *) -Arguments measurable_fst {d1 d2 T1 T2}. -Arguments measurable_snd {d1 d2 T1 T2}. +Lemma RintegralZl d {T : measurableType d} {R : realType} + {mu : measure T R} {D : set T} : d.-measurable D -> + forall f : T -> R, + mu.-integrable D (EFin \o f) -> + forall r : R, (\int[mu]_(x in D) (r * f x) = r * \int[mu]_(x in D) f x)%R. +Proof. +move=> mD f intf r. +rewrite /Rintegral. +under eq_integral do rewrite EFinM. +rewrite integralZl// fineM//=. +by apply: integral_fune_fin_num. +Qed. -Section mswap. -Context d d' d3 (X : measurableType d) (Y : measurableType d') - (Z : measurableType d3) (R : realType). -Variable k : R.-ker Y * X ~> Z. +(* TODO: naming *) +Lemma cvg_atNP {T : topologicalType} {R : numFieldType} (f : R -> T) (a : R) (l : T) : + f x @[x --> a] --> l <-> (f \o -%R) x @[x --> (- a)%R] --> l. +Proof. +rewrite nbhsN. +have <-// : f x @[x --> a] = fmap [eta f \o -%R] ((- x)%R @[x --> a]). +by apply/seteqP; split=> A; move=> [/= e e0 H]; exists e => //= B /H/=; rewrite opprK. +Qed. -Definition mswap xy U := k (swap xy) U. +Lemma derivable_oo_bnd_id {R : numFieldType} (a b : R) : + derivable_oo_continuous_bnd (@id R^o) a b. +Proof. +by split => //; + [exact/cvg_at_right_filter/cvg_id|exact/cvg_at_left_filter/cvg_id]. +Qed. + +Lemma derivable_oo_bndN {R : realFieldType} (f : R -> R^o) a b : + derivable_oo_continuous_bnd f (- a) (- b) -> + derivable_oo_continuous_bnd (f \o -%R) b a. +Proof. +move=> [dF cFa cFb]. +have oppK : (-%R \o -%R) = @id R by apply/funext => x/=; rewrite opprK. +split. +- move=> x xba; apply/derivable1_diffP. + apply/(@differentiable_comp _ _ R^o _ -%R f x) => //. + by apply/derivable1_diffP/dF; rewrite oppr_itvoo 2!opprK. +- by apply/cvg_at_rightNP; rewrite -compA oppK. +- by apply/cvg_at_leftNP; rewrite -compA oppK. +Qed. -Let mswap0 xy : mswap xy set0 = 0. -Proof. done. Qed. +Module increasing_change_of_variables_from_decreasing. +Section lt0. +Context {R : realType}. +Notation mu := lebesgue_measure. +Implicit Types (F G f : R -> R) (a b : R). -Let mswap_ge0 x U : 0 <= mswap x U. -Proof. done. Qed. +Lemma continuous_withinN f a b : (a < b)%R -> + {within `[(- b)%R, (- a)%R], continuous (f \o -%R)} -> + {within `[a, b], continuous f}. +Proof. +move=> ab cf. +- apply/continuous_within_itvP (* TODO: us [/\ ...] *) => //. +- split; rewrite -ltrN2 in ab. + + move=> x xab. + move/continuous_within_itvP : cf => /(_ ab) [cf _ _]. + rewrite (_ : f = (f \o -%R) \o -%R); last first. + by apply/funext => y; rewrite /= opprK. + apply: continuous_comp; first exact: (@opp_continuous _ R^o). + by apply: cf; rewrite -oppr_itvoo opprK. + + move/continuous_within_itvP : cf => /(_ ab) [_ _ cf]. + apply/cvg_at_rightNP. + by rewrite /= opprK in cf. + + move/continuous_within_itvP : cf => /(_ ab) [_ cf _]. + apply/cvg_at_leftNP. + by rewrite /= opprK in cf. +Qed. -Let mswap_sigma_additive x : semi_sigma_additive (mswap x). -Proof. exact: measure_semi_sigma_additive. Qed. +Lemma oppr_change (f : R -> R) a b : (a < b)%R -> + {within `[a, b], continuous f} -> + \int[mu]_(x in `[a, b]) (f x)%:E = + \int[mu]_(x in `[-%R b, -%R a]) ((f \o -%R) x)%:E. +Proof. +move=> ab cf. +have dN : ((-%R : R -> R^o)^`() = cst (-1) :> (R -> R))%R. (* TODO: lemma? *) + by apply/funext => x/=; rewrite derive1E deriveN// derive_id. +rewrite integration_by_substitution_decreasing//. +- by apply: eq_integral => /= x _; rewrite dN/= opprK mulr1 -compA/= opprK. +- by move=> x y _ _ yx; rewrite ltrN2. +- by move=> y yab; rewrite dN; exact: cvg_cst. +- by rewrite dN; exact: is_cvg_cst. +- by rewrite dN; exact: is_cvg_cst. +- by apply: (@derivable_oo_bndN _ id) => //; exact: derivable_oo_bnd_id. +- apply: continuous_withinN. + + by rewrite ltrN2. + + rewrite -(_ : f = (f \o -%R) \o -%R)//; last first. + by apply/funext => y; rewrite /= opprK. + by rewrite !opprK. +Qed. -HB.instance Definition _ x := isMeasure.Build _ _ R - (mswap x) (mswap0 x) (mswap_ge0 x) (@mswap_sigma_additive x). +End lt0. +End increasing_change_of_variables_from_decreasing. -Definition mkswap : _ -> {measure set Z -> \bar R} := - fun x => mswap x. +Lemma decreasing_nonincreasing {R : realType} (F : R -> R) (J : interval R) : + {in J &, {homo F : x y /~ (x < y)%R}} -> + {in J &, {homo F : x y /~ (x <= y)%R}}. +Proof. +move=> dF. +move=> x y x01 y01. +by rewrite le_eqVlt => /predU1P[->//|/dF] => /(_ x01 y01)/ltW. +Qed. -Let measurable_fun_kswap U : - measurable U -> measurable_fun setT (mkswap ^~ U). +Lemma derive1_onem {R: realType} : (fun x0 : R => (1 - x0)%R : R^o)^`() = (cst (-1)%R). Proof. -move=> mU. -rewrite [X in measurable_fun _ X](_ : _ = k ^~ U \o @swap _ _)//. -apply measurableT_comp => //=; first exact: measurable_kernel. -exact: measurable_swap. +apply/funext => x. +by rewrite derive1E deriveB// derive_id derive_cst sub0r. +Qed. + +Local Close Scope ereal_scope. +Lemma cvg_comp_filter {R : realType} (f g : R -> R) (r l : R) : + continuous f -> + (f \o g) x @[x --> r] --> l -> + f x @[x --> g r] --> l. +Proof. +move=> cf fgrl. +apply/(@cvgrPdist_le _ R^o) => /= e e0. +have e20 : 0 < e / 2 by rewrite divr_gt0. +move/(@cvgrPdist_le _ R^o) : fgrl => /(_ _ e20) fgrl. +have := cf (g r). +move=> /(@cvgrPdist_le _ R^o) => /(_ _ e20)[x x0]H. +exists (minr x (e/2)). + by rewrite lt_min x0. +move=> z. +rewrite /ball_ /= => grze. +rewrite -[X in X - _](subrK (f (g r))). +rewrite -(addrA _ _ (- f z)). +apply: (le_trans (ler_normD _ _)). +rewrite (splitr e) lerD//. + case: fgrl => d /= d0 K. + apply: K. + by rewrite /ball_/= subrr normr0. +apply: H => /=. +by rewrite (lt_le_trans grze)// ge_min lexx. Qed. +Local Open Scope ereal_scope. -HB.instance Definition _ := isKernel.Build _ _ - (X * Y)%type Z R mkswap measurable_fun_kswap. +Section change_of_variables_onem. +Context {R : realType}. +Let mu := (@lebesgue_measure R). -End mswap. +Lemma onem_change (G : R -> R) (r : R) : + (0 < r <= 1)%R -> + {within `[0%R, r], continuous G} -> + (\int[mu]_(x in `[0%R, r]) (G x)%:E = + \int[mu]_(x in `[(1 - r)%R, 1%R]) (G (1 - x))%:E). +Proof. +move=> r01 cG. +have := @integration_by_substitution_decreasing R (fun x => 1 - x)%R G (1 - r) 1%R. +rewrite opprB subrr addrCA subrr addr0. +move=> ->//. +- apply: eq_integral => x xr. + rewrite !fctE. + by rewrite derive1_onem opprK mulr1. +- rewrite ltrBlDl ltrDr. + by case/andP : r01. +- by move=> x y _ _ xy; rewrite ler_ltB. +- by rewrite derive1_onem; move=> ? ?; apply: cvg_cst. +- by rewrite derive1_onem; exact: is_cvg_cst. +- by rewrite derive1_onem; exact: is_cvg_cst. +- split => /=. + + move=> x xr1. + by apply: derivableB => //. + + apply: cvg_at_right_filter. + rewrite opprB addrCA addrA addrK. + apply: (@cvg_comp_filter _ _ (fun x => 1 - x)%R)=> //=. + move=> x. + apply: (@continuousB _ R^o) => //. + exact: cvg_cst. + under eq_fun do rewrite opprD addrA subrr add0r opprK. + apply: cvg_id. + apply: cvg_at_left_filter. + apply: (@cvgB _ R^o) => //. + exact: cvg_cst. +Qed. -Section mswap_sfinite_kernel. -Variables (d d' d3 : _) (X : measurableType d) (Y : measurableType d') - (Z : measurableType d3) (R : realType). -Variable k : R.-sfker Y * X ~> Z. +Lemma Ronem_change (G : R -> R) (r : R) : + (0 < r <= 1)%R -> + locally_integrable [set: R] G -> + {within `[0%R, r], continuous G} -> + (forall r, lebesgue_measure.-integrable `[0%R, r] (EFin \o G)) -> + (\int[mu]_(x in `[0%R, r]) (G x) = + \int[mu]_(x in `[(1 - r)%R, 1%R]) (G (1 - x)))%R. +Proof. +move=> r01 locG cG iG. +rewrite [in LHS]/Rintegral. +by rewrite onem_change. +Qed. + +End change_of_variables_onem. + +Section factD. -Let mkswap_sfinite : - exists2 k_ : (R.-ker X * Y ~> Z)^nat, - forall n, measure_fam_uub (k_ n) & - forall x U, measurable U -> mkswap k x U = kseries k_ x U. +Let factD' n m : (n`! * m`! <= (n + m).+1`!)%N. Proof. -have [k_ /= kE] := sfinite_kernel k. -exists (fun n => mkswap (k_ n)). - move=> n. - have /measure_fam_uubP[M hM] := measure_uub (k_ n). - by exists M%:num => x/=; exact: hM. -move=> xy U mU. -by rewrite /mswap/= kE. +elim: n m => /= [m|n ih m]. + by rewrite fact0 mul1n add0n factS leq_pmull. +rewrite 2!factS [in X in (_ <= _ * X)%N]addSn -mulnA leq_mul//. +by rewrite ltnS addSnnS leq_addr. Qed. -HB.instance Definition _ := - isSFiniteKernel_subdef.Build _ _ _ Z R (mkswap k) mkswap_sfinite. +Lemma factD n m : (n`! * m.-1`! <= (n + m)`!)%N. +Proof. +case: m => //= [|m]. + by rewrite fact0 muln1 addn0. +by rewrite addnS factD'. +Qed. + +End factD. -End mswap_sfinite_kernel. +Lemma leq_prod2 (x y n m : nat) : (n <= x)%N -> (m <= y)%N -> + (\prod_(m <= i < y) i * \prod_(n <= i < x) i <= \prod_(n + m <= i < x + y) i)%N. +Proof. +move=> nx my; rewrite big_addn -addnBA//. +rewrite [in leqRHS]/index_iota -addnBAC// iotaD big_cat/=. +rewrite mulnC leq_mul//. + by apply: leq_prod; move=> i _; rewrite leq_addr. +rewrite subnKC//. +rewrite -[in leqLHS](add0n m) big_addn. +rewrite [in leqRHS](_ : y - m = ((y - m + x) - x))%N; last first. + by rewrite -addnBA// subnn addn0. +rewrite -[X in iota X _](add0n x) big_addn -addnBA// subnn addn0. +by apply: leq_prod => i _; rewrite leq_add2r leq_addr. +Qed. -Section kswap_finite_kernel_finite. -Context d d' d3 (X : measurableType d) (Y : measurableType d') - (Z : measurableType d3) (R : realType) - (k : R.-fker Y * X ~> Z). +Lemma leq_fact2 (x y n m : nat) : (n <= x) %N -> (m <= y)%N -> + (x`! * y`! * ((n + m).+1)`! <= n`! * m`! * ((x + y).+1)`!)%N. +Proof. +move=> nx my. +rewrite (fact_split nx) -!mulnA leq_mul2l; apply/orP; right. +rewrite (fact_split my) mulnCA -!mulnA leq_mul2l; apply/orP; right. +rewrite [leqRHS](_ : _ = (n + m).+1`! * \prod_((n + m).+2 <= i < (x + y).+2) i)%N; last first. + by rewrite -fact_split// ltnS leq_add. +rewrite mulnA mulnC leq_mul2l; apply/orP; right. +do 2 rewrite -addSn -addnS. +exact: leq_prod2. +Qed. -Let mkswap_finite : measure_fam_uub (mkswap k). +Lemma bounded_norm_expn_onem {R : realType} (a b : nat) : + [bounded `|x ^+ a * (1 - x) ^+ b|%R : R^o | x in (`[0%R, 1%R]%classic : set R)]. Proof. -have /measure_fam_uubP[r hr] := measure_uub k. -apply/measure_fam_uubP; exists (PosNum [gt0 of r%:num%R]) => x /=. -exact: hr. +exists 1%R; split; [by rewrite num_real|move=> x x1 /= y]. +rewrite in_itv/= => /andP[y0 y1]. +rewrite ger0_norm// ger0_norm; last first. + by rewrite mulr_ge0 ?exprn_ge0// subr_ge0. +rewrite (le_trans _ (ltW x1))// mulr_ile1 ?exprn_ge0//. +- by rewrite subr_ge0. +- by rewrite exprn_ile1. +- rewrite exprn_ile1 ?subr_ge0//. + by rewrite lerBlDl addrC -lerBlDl subrr. +Qed. + +Lemma measurable_fun_expn_onem {R : realType} a b : + measurable_fun setT (fun x : R => x ^+ a * `1-x ^+ b)%R. +Proof. +apply/measurable_funM => //; apply/measurable_funX => //. +exact: measurable_funB. +Qed. + +Section ubeta_nat_pdf. +Local Open Scope ring_scope. +Context {R : realType}. +Variables a b : nat. + +(* unnormalized pdf *) +(*Definition ubeta_nat_pdf (t : R) := + if (0 <= t <= 1)%R then (t ^+ a.-1 * (`1-t) ^+ b.-1)%R else 0%R. + +Lemma ubeta_nat_pdf_ge0 t : 0 <= ubeta_nat_pdf t. +Proof. +rewrite /ubeta_nat_pdf; case: ifPn => // /andP[t0 t1]. +by rewrite mulr_ge0// exprn_ge0// onem_ge0. +Qed. + +Lemma ubeta_nat_pdf_le1 t : ubeta_nat_pdf t <= 1. +Proof. +rewrite /ubeta_nat_pdf; case: ifPn => // /andP[t0 t1]. +by rewrite mulr_ile1// ?(exprn_ge0,onem_ge0,exprn_ile1,onem_le1). +Qed. + +Lemma measurable_ubeta_nat_pdf : measurable_fun setT ubeta_nat_pdf. +Proof. +rewrite /ubeta_nat_pdf /=; apply: measurable_fun_if => //=; last first. + by rewrite setTI; apply: measurable_funTS; exact: measurable_fun_expn_onem. +by apply: measurable_and => /=; exact: measurable_fun_ler. +Qed. + +Local Notation mu := lebesgue_measure. + +Lemma integral_ubeta_nat_pdf U : + (\int[mu]_(x in U) (ubeta_nat_pdf x)%:E = + \int[mu]_(x in U `&` `[0%R, 1%R]) (ubeta_nat_pdf x)%:E)%E. +Proof. +rewrite [RHS]integral_mkcondr/=; apply: eq_integral => x xU. +rewrite patchE; case: ifPn => //. +rewrite notin_setE/= in_itv/= => /negP. +rewrite negb_and -!ltNge => /orP[x0|x1]. + by rewrite /ubeta_nat_pdf leNgt x0/=. +by rewrite /ubeta_nat_pdf !leNgt x1/= andbF. +Qed. + +Lemma integral_ubeta_nat_pdfT : + (\int[mu]_x (ubeta_nat_pdf x)%:E = + \int[mu]_(x in `[0%R, 1%R]) (ubeta_nat_pdf x)%:E)%E. +Proof. by rewrite integral_ubeta_nat_pdf/= setTI. Qed.*) + +End ubeta_nat_pdf. + +(*Lemma ubeta_nat_pdf11 {R : realType} (x : R) : (0 <= x <= 1)%R -> + ubeta_nat_pdf 1 1 x = 1%R. +Proof. +move=> x01. +by rewrite /ubeta_nat_pdf !expr0 mulr1 x01. +Qed. + +(* normalization constant *) +Definition beta_nat_norm {R : realType} (a b : nat) : R := + fine (\int[@lebesgue_measure R]_x (ubeta_nat_pdf a b x)%:E).*) + +Section beta_nat_Gamma. +Context {R : realType}. +(* +Let mu := @lebesgue_measure R. + +Let B (a b : nat) : \bar R := + \int[mu]_(x in `[0%R, 1%R]%classic) (ubeta_nat_pdf a b x)%:E. +*) +End beta_nat_Gamma. + +(* +Lemma integral_beta_nat_normTE {R : realType} (a b : nat) : + beta_nat_norm a b = + fine (\int[lebesgue_measure]_(t in `[0%R, 1%R]) (t^+a.-1 * (`1-t)^+b.-1)%:E) :> R. +Proof. +rewrite /beta_nat_norm /ubeta_nat_pdf [in RHS]integral_mkcond/=; congr fine. +by apply: eq_integral => /= x _; rewrite patchE mem_setE in_itv/=; case: ifPn. +Qed.*) + +Lemma onemXn_derivable {R : realType} n (x : R) : + derivable (fun y : R^o => `1-y ^+ n : R^o)%R x 1. +Proof. +have := @derivableX R R^o (@onem R) n x 1%R. +rewrite fctE. +apply. +exact: derivableB. +Qed. + +Lemma deriveX_idfun {R : realType} n x : + 'D_1 (@GRing.exp R^o ^~ n.+1) x = n.+1%:R *: (x ^+ n)%R. +Proof. by rewrite exp_derive /GRing.scale/= mulr1. Qed. + +Lemma derive1Mr [R : realFieldType] [f : R^o -> R^o] [x r : R^o] : + derivable f x 1 -> ((fun x => f x * r)^`() x = (r * f^`() x)%R :> R)%R. +Proof. +move=> fx1. +rewrite derive1E (deriveM fx1); last by []. +by rewrite -derive1E derive1_cst scaler0 add0r derive1E. +Qed. + +Lemma derive1Ml [R : realFieldType] [f : R^o -> R^o] [x r : R^o] : + derivable f x 1 -> ((fun x => r * f x)^`() x = (r * f^`() x)%R :> R)%R. +Proof. +under eq_fun do rewrite mulrC. +exact: derive1Mr. +Qed. + +Lemma decreasing_onem {R : numDomainType} : {homo (fun x : R => (1 - x)%R) : x y /~ (x < y)%R}. +Proof. +move=> b a ab. +by rewrite -ltrN2 !opprB ltr_leB. +Qed. + +Lemma continuous_onemXn {R : realType} (n : nat) x : + {for x, continuous (fun y : R => `1-y ^+ n)%R}. +Proof. +apply: (@continuous_comp _ _ _ (@onem R) (fun x => GRing.exp x n)). + by apply: (@cvgB _ R^o); [exact: cvg_cst|exact: cvg_id]. +exact: exprn_continuous. +Qed. + +Local Close Scope ereal_scope. + +(* we define a function to help formalizing the beta distribution *) +Section XMonemX. +Context {R : numDomainType}. + +Definition XMonemX a b := fun x : R => x ^+ a * `1-x ^+ b. + +Lemma XMonemX_ge0 a b x : x \in `[0%R, 1%R] -> 0 <= XMonemX a b x :> R. +Proof. +rewrite in_itv/= => /andP[x0 x1]. +by rewrite /XMonemX mulr_ge0// exprn_ge0// subr_ge0. +Qed. + +Lemma XMonemX0 n x : XMonemX 0 n x = `1-x ^+ n :> R. +Proof. by rewrite /XMonemX/= expr0 mul1r. Qed. + +Lemma XMonemX0' n x : XMonemX n 0 x = x ^+ n :> R. +Proof. by rewrite /XMonemX/= expr0 mulr1. Qed. + +Lemma XMonemX00 x : XMonemX 0 0 x = 1 :> R. +Proof. by rewrite XMonemX0 expr0. Qed. + +Lemma XMonemXC a b x : XMonemX a b (1 - x) = XMonemX b a x :> R. +Proof. +by rewrite /XMonemX [in LHS]/onem opprB addrCA subrr addr0 mulrC. +Qed. + +End XMonemX. + +Lemma continuous_XMonemX {R : realType} a b : + continuous (XMonemX a b : R -> R). +Proof. +by move=> x; apply: cvgM; [exact: exprn_continuous|exact: continuous_onemXn]. +Qed. + +Lemma within_continuous_XMonemX {R : realType} a b (A : set R) : + {within A, continuous (XMonemX a b : R -> R)}. +Proof. +by apply: continuous_in_subspaceT => x _; exact: continuous_XMonemX. +Qed. + +Lemma bounded_XMonemX {R : realType} (a b : nat) : + [bounded XMonemX a b x : R^o | x in (`[0, 1]%classic : set R)]. +Proof. +exists 1%R; split; [by rewrite num_real|move=> x x1 /= y y01]. +rewrite ger0_norm//; last by rewrite XMonemX_ge0. +move: y01; rewrite in_itv/= => /andP[? ?]. +rewrite (le_trans _ (ltW x1))// mulr_ile1 ?exprn_ge0//. +- by rewrite subr_ge0. +- by rewrite exprn_ile1. +- by rewrite exprn_ile1 ?subr_ge0// lerBlDl addrC -lerBlDl subrr. +Qed. + +Lemma measurable_fun_XMonemX {R : realType} a b (A : set R) : + measurable_fun A (XMonemX a b). +Proof. +apply/measurable_funM => //; apply/measurable_funX => //. +exact: measurable_funB. +Qed. + +Local Open Scope ereal_scope. + +Lemma integral_exprn {R : realType} (n : nat) : + fine (\int[lebesgue_measure]_(x in `[0%R, 1%R]) (x ^+ n)%:E) = n.+1%:R^-1 :> R. +Proof. +pose F (x : R) : R^o := (n.+1%:R^-1 * x ^+ n.+1)%R. +have cX m : {in `[0%R, 1%R], continuous (fun x : R => x ^+ m)%R}. + by move=> x x01; exact: exprn_continuous. +have cF0 : {for 0%R, continuous F}. + apply: continuousM; first exact: cvg_cst. + by apply: cX; rewrite /= in_itv/= lexx ler01. +have cF1 : {for 1%R, continuous F}. + apply: continuousM; first exact: cvg_cst. + by apply: cX; rewrite /= in_itv/= lexx ler01. +have dcF : derivable_oo_continuous_bnd F 0 1. + split. + - by move=> x x01; apply: derivableM => //; exact: exprn_derivable. + - apply: continuous_cvg; first exact: mulrl_continuous. + by apply/cvg_at_right_filter/cX; rewrite in_itv/= lexx ler01. + - apply: continuous_cvg; first exact: mulrl_continuous. + by apply/cvg_at_left_filter/cX; rewrite in_itv/= lexx ler01. +have dFE : {in `]0%R, 1%R[, F^`() =1 (fun x : R => x ^+ n : R)%R}. + move=> x x01. + rewrite derive1Ml; last exact: exprn_derivable. + by rewrite derive1E deriveX_idfun mulrA mulVf// mul1r. +rewrite (@continuous_FTC2 _ (fun x : R => x ^+ n)%R F)//. + by rewrite /F/= expr1n expr0n/= mulr1 mulr0 subr0. +by apply: continuous_subspaceT; exact: exprn_continuous. +Qed. + +Lemma derivable_oo_continuous_bnd_onemXnMr {R : realType} (n : nat) (r : R) : + derivable_oo_continuous_bnd (fun x : R => `1-x ^+ n.+1 * r : R^o)%R 0 1. +Proof. +split. +- by move=> x x01; apply: derivableM => //=; exact: onemXn_derivable. +- apply: cvgM; last exact: cvg_cst. + apply: cvg_at_right_filter. + apply: (@cvg_comp _ _ _ (fun x => 1 - x)%R (fun x => GRing.exp x n.+1)%R). + by apply: (@cvgB _ R^o); [exact: cvg_cst|exact: cvg_id]. + exact: exprn_continuous. +- apply: cvg_at_left_filter. + apply: cvgM; last exact: cvg_cst. + apply: (@cvg_comp _ _ _ (fun x => 1 - x)%R (fun x => GRing.exp x n.+1)%R). + by apply: (@cvgB _ R^o); [exact: cvg_cst|exact: cvg_id]. + exact: exprn_continuous. +Qed. + +Lemma derive_onemXn {R : realType} (n : nat) x : + ((fun y : R => `1-y ^+ n.+1 : R^o)^`() x = - n.+1%:R * `1-x ^+ n)%R. +Proof. +rewrite (@derive1_comp _ (@onem R) (fun x => GRing.exp x n.+1))//; last first. + exact: exprn_derivable. +rewrite derive1E deriveX_idfun derive1E deriveB//. +by rewrite -derive1E derive1_cst derive_id sub0r mulrN1 [in RHS]mulNr. +Qed. + +Lemma integral_onemXn {R : realType} (n : nat) : + fine (\int[lebesgue_measure]_(x in `[0%R, 1%R]) (`1-x ^+ n)%:E) = n.+1%:R^-1 :> R. +Proof. +rewrite (@continuous_FTC2 _ _ (fun x : R => ((1 - x) ^+ n.+1 / - n.+1%:R))%R)//=. +- rewrite subrr subr0 expr0n/= mul0r expr1n mul1r sub0r. + by rewrite -invrN -2!mulNrn opprK. +- apply: continuous_in_subspaceT. + by move=> x x01; exact: continuous_onemXn. +- exact: derivable_oo_continuous_bnd_onemXnMr. +- move=> x x01. + rewrite derive1Mr//; last exact: onemXn_derivable. + by rewrite derive_onemXn mulrA mulVf// mul1r. +Qed. + +Lemma integrable_XMonemX {R : realType} (a b : nat) : + lebesgue_measure.-integrable `[0%R, 1%R] (fun x : R => (XMonemX a b x)%:E). +Proof. +apply: continuous_compact_integrable => //. + exact: segment_compact. +apply: continuous_in_subspaceT => x _. +exact: continuous_XMonemX. +Qed. + +Lemma Rintegral_onemXn {R : realType} (n : nat) : + (\int[lebesgue_measure]_(x in `[0%R, 1%R]) (`1-x ^+ n))%R = n.+1%:R^-1 :> R. +Proof. +rewrite /Rintegral. +rewrite (@continuous_FTC2 _ _ (fun x : R => ((1 - x) ^+ n.+1 / - n.+1%:R))%R)//=. +- rewrite subrr subr0 expr0n/= mul0r expr1n mul1r sub0r. + by rewrite -invrN -2!mulNrn opprK. +- apply: continuous_in_subspaceT. +- by move=> x x01; exact: continuous_onemXn. +- exact: derivable_oo_continuous_bnd_onemXnMr. +- move=> x x01. + rewrite derive1Mr//; last exact: onemXn_derivable. + by rewrite derive_onemXn mulrA mulVf// mul1r. +Qed. + +(* TODO: move *) +Lemma normr_onem {R : realType} (x : R) : (0 <= x <= 1 -> `| `1-x | <= 1)%R. +Proof. +move=> /andP[x0 x1]; rewrite ler_norml; apply/andP; split. + by rewrite lerBrDl lerBlDr (le_trans x1)// lerDl. +by rewrite lerBlDr lerDl. +Qed. + +Local Open Scope ereal_scope. + +Local Open Scope ring_scope. + +Section XMonemX01. +Local Open Scope ring_scope. +Context {R : realType}. +Variables a b : nat. + +Definition XMonemX01 := (@XMonemX R a.-1 b.-1) \_ `[0, 1]. + +Lemma XMonemX01_ge0 t : 0 <= XMonemX01 t. +Proof. +rewrite /XMonemX01 patchE ; case: ifPn => //. +rewrite inE/= in_itv/= => /andP[t0 t1]. +by rewrite mulr_ge0// exprn_ge0// onem_ge0. +Qed. + +Lemma XMonemX01_le1 t : XMonemX01 t <= 1. +Proof. +rewrite /XMonemX01 patchE ; case: ifPn => //. +rewrite inE/= in_itv/= => /andP[t0 t1]. +by rewrite mulr_ile1// ?(exprn_ge0,onem_ge0,exprn_ile1,onem_le1). +Qed. + +Lemma measurable_XMonemX01 : measurable_fun [set: R] XMonemX01. +Proof. +rewrite /XMonemX01 /=; apply/(measurable_restrictT _ _).1 => //. +exact: measurable_fun_XMonemX. +Qed. + +Local Notation mu := lebesgue_measure. + +(* TODO: maybe not that useful *) +Lemma integral_XMonemX01 U : + (\int[mu]_(x in U) (XMonemX01 x)%:E = + \int[mu]_(x in U `&` `[0%R, 1%R]) (XMonemX01 x)%:E)%E. +Proof. +rewrite [RHS]integral_mkcondr /=; apply: eq_integral => x xU /=. +by rewrite /XMonemX01/= restrict_EFin -patch_setI setIid. +Qed. + +End XMonemX01. + +Lemma XMonemX_XMonemX01 {R : realType} a b a' b' (x : R) : (0 < a)%N -> (0 < b)%N -> + x \in `[0%R, 1%R]%classic -> + (XMonemX a' b' x * XMonemX01 a b x = XMonemX01 (a + a') (b + b') x :> R)%R. +Proof. +move=> a0 b0 x01; rewrite /XMonemX01 /= !patchE x01. +rewrite mulrCA -mulrA -exprD mulrA -exprD. +congr (_ ^+ _ * _ ^+ _)%R. + by rewrite addnC -!subn1 subDnCA. +by rewrite -!subn1 subDnCA. +Qed. + +Lemma XMonemX01_11 {R : realType} (x : R) : (0 <= x <= 1)%R -> + XMonemX01 1 1 x = 1%R. +Proof. +by move=> x01; rewrite /XMonemX01 patchE mem_setE in_itv/= x01/= XMonemX00. +Qed. + +(* normalization constant *) +Section betafun. +Context {R : realType}. +Notation mu := (@lebesgue_measure R). +Local Open Scope ring_scope. + +Definition betafun (a b : nat) : R := (\int[mu]_x (XMonemX01 a b x))%R. + +Lemma betafun0 (b : nat) : (0 < b)%N -> betafun 0 b = b%:R ^-1:> R. +Proof. +move=> b0. +rewrite -[LHS]Rintegral_mkcond. +under eq_Rintegral do rewrite XMonemX0. +by rewrite Rintegral_onemXn// prednK. +Qed. + +Lemma betafun00 : betafun 0 0 = 1%R :> R. +Proof. +rewrite -[LHS]Rintegral_mkcond. +under eq_Rintegral do rewrite XMonemX00. +rewrite /Rintegral. +rewrite integral_cst/= ?mul1e; last by exact: measurable_itv. +by rewrite lebesgue_measure_itv/= lte_fin ltr01 -EFinB subr0. +Qed. + +Lemma betafun_sym (a b : nat) : betafun a b = betafun b a :> R. +Proof. +rewrite -[LHS]Rintegral_mkcond. +rewrite Ronem_change//=; last 4 first. + by rewrite ltr01 lexx. + split. + - exact: measurable_fun_XMonemX. + - exact: openT. + - move=> K _ cK. + by have /integrableP[] := @continuous_compact_integrable R _ K cK + (@within_continuous_XMonemX R a.-1 b.-1 K). + apply: continuous_subspaceT. + by move=> x x01; exact: continuous_XMonemX. + move=> r. + exact: (@continuous_compact_integrable R _ _ + (@segment_compact _ _ _) (@within_continuous_XMonemX R a.-1 b.-1 `[0%R, r])). +rewrite subrr. +rewrite -[RHS]Rintegral_mkcond. +apply: eq_Rintegral => x x01. +by rewrite XMonemXC. +Qed. + +Lemma betafunS (a b : nat) : + (betafun a.+2 b.+1 = a.+1%:R / b.+1%:R * betafun a.+1 b.+2 :> R)%R. +Proof. +rewrite -[LHS]Rintegral_mkcond. +rewrite (@Rintegration_by_parts _ _ + (fun x => `1-x ^+ b.+1 / - b.+1%:R)%R (fun x => a.+1%:R * x ^+ a)%R); last 7 first. + exact: ltr01. + apply/continuous_subspaceT. + move=> x. + apply: cvgM; [exact: cvg_cst|]. + exact: exprn_continuous. + split. + by move=> x x01; exact: exprn_derivable. + by apply: cvg_at_right_filter; exact: exprn_continuous. + by apply: cvg_at_left_filter; exact: exprn_continuous. + by move=> x x01; rewrite derive1E deriveX_idfun. + apply/continuous_subspaceT. + by move=> x x01; exact: continuous_onemXn. + exact: derivable_oo_continuous_bnd_onemXnMr. + move=> x x01. + rewrite derive1Mr; last exact: onemXn_derivable. + by rewrite derive_onemXn mulrA mulVf// mul1r. +rewrite {1}/onem !(expr1n,mul1r,expr0n,subr0,subrr,mul0r,oppr0)/=. +rewrite sub0r. +transitivity (a.+1%:R / b.+1%:R * (\int[lebesgue_measure]_(x in `[0, 1]) (XMonemX a b.+1 x)) : R)%R. + under [in LHS]eq_Rintegral. + move=> x x01. + rewrite mulrA mulrC mulrA (mulrA _ a.+1%:R) -(mulrA (_ * _)%R). + over. + rewrite /=. + rewrite RintegralZl//=; last exact: integrable_XMonemX. + by rewrite -mulNrn -2!mulNr -invrN -mulNrn opprK (mulrC _ a.+1%:R)//=. +by rewrite Rintegral_mkcond. +Qed. + +Lemma betafunSS (a b : nat) : + (betafun a.+1 b.+1 = + a`!%:R / (\prod_(b.+1 <= i < (a + b).+1) i)%:R * betafun 1 (a + b).+1 :> R)%R. +Proof. +elim: a b => [b|a ih b]. + by rewrite fact0 mul1r add0n /index_iota subnn big_nil invr1 mul1r. +rewrite betafunS. +rewrite ih. +rewrite !mulrA. +congr *%R; last by rewrite addSnnS. +rewrite -mulrA. +rewrite mulrCA. +rewrite 2!mulrA. +rewrite -natrM. +rewrite (mulnC a`!). +rewrite -factS. +rewrite -mulrA. +rewrite -invfM. +rewrite big_add1. +rewrite [in RHS]big_nat_recl/=; last by rewrite addSn ltnS leq_addl. +by rewrite -natrM addSnnS. +Qed. + +Lemma betafun1S (n : nat) : (betafun 1 n.+1 = n.+1%:R^-1 :> R)%R. +Proof. +rewrite /betafun -Rintegral_mkcond. +under eq_Rintegral do rewrite XMonemX0. +by rewrite Rintegral_onemXn. +Qed. + +Lemma betafun_fact (a b : nat) : + (betafun a.+1 b.+1 = (a`! * b`!)%:R / (a + b).+1`!%:R :> R)%R. +Proof. +rewrite betafunSS betafun1S. +rewrite natrM -!mulrA; congr *%R. +(* (b+1 b+2 ... b+1 b+a)^-1 / (a+b+1) = b! / (a+b+1)! *) +rewrite factS. +rewrite [in RHS]mulnC. +rewrite natrM. +rewrite invfM. +rewrite mulrA; congr (_ / _). +rewrite -(@invrK _ b`!%:R) -invfM; congr (_^-1). +apply: (@mulfI _ b`!%:R). + by rewrite gt_eqF// ltr0n fact_gt0. +rewrite mulrA divff// ?gt_eqF// ?ltr0n ?fact_gt0//. +rewrite mul1r. +rewrite [in RHS]fact_prod. +rewrite -natrM; congr (_%:R). +rewrite fact_prod -big_cat/=. +by rewrite /index_iota subn1 -iotaD subn1/= subSS addnK addnC. +Qed. + +Lemma betafunE (a b : nat) : betafun a b = + if (a == 0)%N && (0 < b)%N then + b%:R^-1 + else if (b == 0)%N && (0 < a)%N then + a%:R^-1 + else + a.-1`!%:R * b.-1`!%:R / (a + b).-1`!%:R :> R. +Proof. +case: a => [|a]. + rewrite eqxx/=; case: ifPn => [|]. + by case: b => [|b _] //; rewrite betafun0. + rewrite -leqNgt leqn0 => /eqP ->. + by rewrite betafun00 eqxx ltnn/= fact0 mul1r divr1. +case: b => [|b]. + by rewrite betafun_sym betafun0// fact0 addn0/= mulr1 divff. +by rewrite betafun_fact/= natrM// -addnE addnS. +Qed. + +Lemma betafun_gt0 (a b : nat) : (0 < betafun a b :> R)%R. +Proof. +rewrite betafunE. +case: ifPn => [/andP[_ b0]|]; first by rewrite invr_gt0 ltr0n. +rewrite negb_and => /orP[a0|]. + case: ifPn => [/andP[_]|]; first by rewrite invr_gt0// ltr0n. + rewrite negb_and => /orP[b0|]. + by rewrite divr_gt0// ?mulr_gt0 ?ltr0n ?fact_gt0. + by rewrite -leqNgt leqn0 (negbTE a0). +rewrite -leqNgt leqn0 => /eqP ->. +rewrite eqxx/=. +case: ifPn; first by rewrite invr_gt0 ltr0n. +rewrite -leqNgt leqn0 => /eqP ->. +by rewrite fact0 mul1r divr1. +Qed. + +Lemma betafun_ge0 (a b : nat) : (0 <= betafun a b :> R)%R. +Proof. exact/ltW/betafun_gt0. Qed. + +Lemma betafun11 : betafun 1 1 = 1%R :> R. +Proof. by rewrite (betafun1S O) invr1. Qed. + +(* NB: this is not exactly betafun because EFin *) +Definition betafunEFin a b : \bar R := \int[mu]_x (XMonemX01 a b x)%:E. + +(* TODO: rev eq *) +Lemma betafunEFinT a b : + (betafunEFin a b = \int[mu]_(x in `[0%R, 1%R]) (XMonemX01 a b x)%:E)%E. +Proof. by rewrite /betafunEFin integral_XMonemX01/= setTI. Qed. + +Lemma betafunEFin_lty a b : (betafunEFin a b < +oo)%E. +Proof. +have := betafun_gt0 a b; rewrite /betafun /Rintegral /betafunEFin. +by case: (integral _ _ _) => [r _| |//]; rewrite ?ltxx ?ltry. +Qed. + +Lemma betafunEFin_fin_num a b : betafunEFin a b \is a fin_num. +Proof. +rewrite ge0_fin_numE ?betafunEFin_lty//. +by apply: integral_ge0 => //= x _; rewrite lee_fin XMonemX01_ge0. +Qed. + +Lemma betafunEFinE a b : (betafunEFin a b = (betafun a b)%:E :> \bar R)%E. +Proof. by rewrite -[LHS]fineK ?betafunEFin_fin_num. Qed. + +Lemma integrable_XMonemX01 a b : mu.-integrable setT (EFin \o XMonemX01 a b). +Proof. +apply/integrableP; split. + by apply/EFin_measurable_fun; exact: measurable_XMonemX01. +under eq_integral. + move=> /= x _. + rewrite ger0_norm//; last by rewrite XMonemX01_ge0. + over. +exact: betafunEFin_lty. +Qed. + +End betafun. + +(* normalized pdf for the beta distribution *) +Section beta_pdf. +Local Open Scope ring_scope. +Context {R : realType}. +Variables a b : nat. + +Definition beta_pdf t : R := XMonemX01 a b t / betafun a b. + +Lemma measurable_beta_pdf : measurable_fun setT beta_pdf. +Proof. by apply: measurable_funM => //; exact: measurable_XMonemX01. Qed. + +Lemma beta_pdf_ge0 t : 0 <= beta_pdf t. +Proof. +by rewrite /beta_pdf divr_ge0//; [exact: XMonemX01_ge0|exact: betafun_ge0]. +Qed. + +Lemma beta_pdf_le_betafunV x : beta_pdf x <= (betafun a b)^-1. +Proof. +rewrite /beta_pdf ler_pdivrMr ?betafun_gt0// mulVf ?gt_eqF ?betafun_gt0//. +exact: XMonemX01_le1. +Qed. + +Local Notation mu := lebesgue_measure. + +(* TODO: really useful? *) +Lemma int_beta_pdf01 : + (\int[mu]_(x in `[0%R, 1%R]) (beta_pdf x)%:E = + \int[mu]_x (beta_pdf x)%:E :> \bar R)%E. +Proof. +rewrite /beta_pdf. +under eq_integral do rewrite EFinM. +rewrite /=. +rewrite ge0_integralZr//=; last 3 first. + apply: measurable_funTS => /=; apply/measurable_EFinP => //. + exact: measurable_XMonemX01. + by move=> x _; rewrite lee_fin XMonemX01_ge0. + by rewrite lee_fin invr_ge0// betafun_ge0. +rewrite -betafunEFinT -ge0_integralZr//=. +- by apply/measurableT_comp => //; exact: measurable_XMonemX01. +- by move=> x _; rewrite lee_fin XMonemX01_ge0. +- by rewrite lee_fin invr_ge0// betafun_ge0. +Qed. + +Lemma integrable_beta_pdf : mu.-integrable setT (EFin \o beta_pdf). +Proof. +apply/integrableP; split. + by apply/measurable_EFinP; exact: measurable_beta_pdf. +under eq_integral. + move=> /= x _. + rewrite ger0_norm//; last by rewrite beta_pdf_ge0. + over. +rewrite -int_beta_pdf01. +apply: (@le_lt_trans _ _ (\int[mu]_(x in `[0%R, 1%R]) (betafun a b)^-1%:E)%E). + apply: ge0_le_integral => //=. + - by move=> x _; rewrite lee_fin beta_pdf_ge0. + - by apply/measurable_funTS/measurable_EFinP => /=; exact: measurable_beta_pdf. + - by move=> x _; rewrite lee_fin invr_ge0// betafun_ge0. + - by move=> x _; rewrite lee_fin beta_pdf_le_betafunV. +rewrite integral_cst//= lebesgue_measure_itv//=. +by rewrite lte01 oppr0 adde0 mule1 ltry. +Qed. + +Local Open Scope ring_scope. +Lemma bounded_beta_pdf_01 : + [bounded beta_pdf x : R^o | x in `[0%R, 1%R]%classic : set R]. +Proof. +exists (betafun a b)^-1; split; first by rewrite num_real. +move=> // y y1. +near=> M => /=. +rewrite (le_trans _ (ltW y1))//. +near: M. +move=> M /=. +rewrite in_itv/= => /andP[M0 M1]. +rewrite ler_norml; apply/andP; split. + rewrite lerNl (@le_trans _ _ 0%R)// ?invr_ge0 ?betafun_ge0//. + by rewrite lerNl oppr0 beta_pdf_ge0. +rewrite /beta_pdf ler_pdivrMr ?betafun_gt0//. +by rewrite mulVf ?XMonemX01_le1// gt_eqF// betafun_gt0. +Unshelve. all: by end_near. Qed. +Local Close Scope ring_scope. + +End beta_pdf. + +Section beta. +Local Open Scope ring_scope. +Context {R : realType}. +Variables a b : nat. + +Local Notation mu := (@lebesgue_measure R). + +Let beta_num (U : set (measurableTypeR R)) : \bar R := + \int[mu]_(x in U) (XMonemX01 a b x)%:E. + +Let beta_numT : beta_num setT = betafunEFin a b. +Proof. by rewrite /beta_num/= -/(betafunEFin a b) betafunEFinE. Qed. + +Let beta_num_lty U : measurable U -> (beta_num U < +oo)%E. +Proof. +move=> mU. +apply: (@le_lt_trans _ _ (\int[mu]_(x in U `&` `[0%R, 1%R]%classic) 1)%E); last first. + rewrite integral_cst//= ?mul1e//. + rewrite (le_lt_trans (measureIr _ _ _))//= lebesgue_measure_itv//= lte01//. + by rewrite EFinN sube0 ltry. + exact: measurableI. +rewrite /beta_num integral_XMonemX01 ge0_le_integral//=. +- exact: measurableI. +- by move=> x _; rewrite lee_fin XMonemX01_ge0. +- by apply/measurable_funTS/measurableT_comp => //; exact: measurable_XMonemX01. +- by move=> x _; rewrite lee_fin XMonemX01_le1. +Qed. + +Let beta_num0 : beta_num set0 = 0%:E. +Proof. by rewrite /beta_num integral_set0. Qed. + +Let beta_num_ge0 U : (0 <= beta_num U)%E. +Proof. +by rewrite /beta_num integral_ge0//= => x Ux; rewrite lee_fin XMonemX01_ge0. +Qed. + +Let beta_num_sigma_additive : semi_sigma_additive beta_num. +Proof. +move=> /= F mF tF mUF; rewrite /beta_num; apply: cvg_toP. + apply: ereal_nondecreasing_is_cvgn => m n mn. + apply: lee_sum_nneg_natr => // k _ _; apply: integral_ge0 => /= x Fkx. + by rewrite lee_fin; exact: XMonemX01_ge0. +rewrite ge0_integral_bigcup//=. +- by apply/measurable_funTS/measurableT_comp => //; exact: measurable_XMonemX01. +- by move=> x _; rewrite lee_fin XMonemX01_ge0. +Qed. + +HB.instance Definition _ := isMeasure.Build _ _ _ beta_num + beta_num0 beta_num_ge0 beta_num_sigma_additive. + +Definition beta_prob := + @mscale _ _ _ (invr_nonneg (NngNum (betafun_ge0 a b))) beta_num. + +HB.instance Definition _ := Measure.on beta_prob. + +Let beta_prob_setT : beta_prob setT = 1%:E. +Proof. +rewrite /beta_prob /= /mscale /= beta_numT betafunEFinE//. +by rewrite -EFinM mulVf// gt_eqF// betafun_gt0. Qed. HB.instance Definition _ := - Kernel_isFinite.Build _ _ _ Z R (mkswap k) mkswap_finite. + @Measure_isProbability.Build _ _ _ beta_prob beta_prob_setT. + +Lemma beta_prob01 : beta_prob `[0, 1] = 1%:E. +Proof. +rewrite /beta_prob /= /mscale/= /beta_num -betafunEFinT betafunEFinE//. +by rewrite -EFinM mulVf// gt_eqF// betafun_gt0. +Qed. -End kswap_finite_kernel_finite. +Lemma beta_prob_fin_num U : measurable U -> beta_prob U \is a fin_num. +Proof. +move=> mU; rewrite ge0_fin_numE//. +rewrite /beta_prob/= /mscale/= /beta_num lte_mul_pinfty//. + by rewrite lee_fin// invr_ge0 betafun_ge0. +apply: (@le_lt_trans _ _ (betafunEFin a b)). + apply: ge0_subset_integral => //=. + by apply/measurable_EFinP; exact: measurable_XMonemX01. + by move=> x _; rewrite lee_fin XMonemX01_ge0. +by rewrite betafunEFin_lty. +Qed. -Notation "l .; k" := (mkcomp l (mkswap k)) : ereal_scope. +Lemma integral_beta_pdf U : measurable U -> + (\int[mu]_(x in U) (beta_pdf a b x)%:E = beta_prob U :> \bar R)%E. +Proof. +move=> mU. +rewrite /beta_pdf. +under eq_integral do rewrite EFinM/=. +rewrite ge0_integralZr//=. +- by rewrite /beta_prob/= /mscale/= muleC. +- by apply/measurable_funTS/measurableT_comp => //; exact: measurable_XMonemX01. +- by move=> x _; rewrite lee_fin XMonemX01_ge0. +- by rewrite lee_fin invr_ge0// betafun_ge0. +Qed. -Section letin'. -Variables (d d' d3 : _) (X : measurableType d) (Y : measurableType d') - (Z : measurableType d3) (R : realType). +End beta. +Arguments beta_prob {R}. -Definition letin' (l : R.-sfker X ~> Y) (k : R.-sfker Y * X ~> Z) := - locked [the R.-sfker X ~> Z of l .; k]. +Lemma integral_beta_prob_bernoulli_lty {R : realType} a b (f : R -> R) U : + measurable_fun setT f -> + (forall x, x \in `[0%R, 1%R] -> 0 <= f x <= 1)%R -> + (\int[beta_prob a b]_x `|bernoulli (f x) U| < +oo :> \bar R)%E. +Proof. +move=> mf /= f01. +apply: (@le_lt_trans _ _ (\int[beta_prob a b]_x cst 1 x))%E. + apply: ge0_le_integral => //=. + apply: measurableT_comp => //=. + by apply: (measurableT_comp (measurable_bernoulli2 _)). + by move=> x _; rewrite gee0_abs// probability_le1. +by rewrite integral_cst//= mul1e -ge0_fin_numE// beta_prob_fin_num. +Qed. -Lemma letin'E (l : R.-sfker X ~> Y) (k : R.-sfker Y * X ~> Z) x U : - letin' l k x U = \int[l x]_y k (y, x) U. -Proof. by rewrite /letin'; unlock. Qed. +Lemma integral_beta_prob_bernoulli_onemX_lty {R : realType} n a b U : + (\int[beta_prob a b]_x `|bernoulli (`1-x ^+ n) U| < +oo :> \bar R)%E. +Proof. +apply: integral_beta_prob_bernoulli_lty => //=. + by apply: measurable_funX => //; exact: measurable_funB. +move=> x; rewrite in_itv/= => /andP[x0 x1]. +rewrite exprn_ge0 ?subr_ge0//= exprn_ile1// ?subr_ge0//. +by rewrite lerBlDl -lerBlDr subrr. +Qed. -Lemma letin'_letin (l : R.-sfker X ~> Y) (k : R.-sfker Y * X ~> Z) : - letin' l k = letin l (mkswap k). -Proof. by rewrite /letin'; unlock. Qed. +Lemma integral_beta_prob_bernoulli_onem_lty {R : realType} n a b U : + (\int[beta_prob a b]_x `|bernoulli (1 - `1-x ^+ n) U| < +oo :> \bar R)%E. +Proof. +apply: integral_beta_prob_bernoulli_lty => //=. + apply: measurable_funB => //. + by apply: measurable_funX => //; exact: measurable_funB. +move=> x; rewrite in_itv/= => /andP[x0 x1]. +rewrite -lerBlDr opprK add0r. +rewrite andbC lerBlDl -lerBlDr subrr. +rewrite exprn_ge0 ?subr_ge0//= exprn_ile1// ?subr_ge0//. +by rewrite lerBlDl -lerBlDr subrr. +Qed. -End letin'. +Local Open Scope ring_scope. -Section letin'C. -Import Notations. -Context d d1 d' (X : measurableType d) (Y : measurableType d1) - (Z : measurableType d') (R : realType). -Variables (t : R.-sfker Z ~> X) - (u' : R.-sfker X * Z ~> Y) - (u : R.-sfker Z ~> Y) - (t' : R.-sfker Y * Z ~> X) - (tt' : forall y, t =1 fun z => t' (y, z)) - (uu' : forall x, u =1 fun z => u' (x, z)). - -Definition T' z : set X -> \bar R := t z. -Let T0 z : (T' z) set0 = 0. Proof. by []. Qed. -Let T_ge0 z x : 0 <= (T' z) x. Proof. by []. Qed. -Let T_semi_sigma_additive z : semi_sigma_additive (T' z). -Proof. exact: measure_semi_sigma_additive. Qed. -HB.instance Definition _ z := @isMeasure.Build _ X R (T' z) (T0 z) (T_ge0 z) - (@T_semi_sigma_additive z). - -Let sfinT z : sfinite_measure (T' z). Proof. exact: sfinite_kernel_measure. Qed. -HB.instance Definition _ z := @isSFinite.Build _ X R (T' z) (sfinT z). - -Definition U' z : set Y -> \bar R := u z. -Let U0 z : (U' z) set0 = 0. Proof. by []. Qed. -Let U_ge0 z x : 0 <= (U' z) x. Proof. by []. Qed. -Let U_semi_sigma_additive z : semi_sigma_additive (U' z). -Proof. exact: measure_semi_sigma_additive. Qed. -HB.instance Definition _ z := @isMeasure.Build _ Y R (U' z) (U0 z) (U_ge0 z) - (@U_semi_sigma_additive z). - -Let sfinU z : sfinite_measure (U' z). Proof. exact: sfinite_kernel_measure. Qed. -HB.instance Definition _ z := @isSFinite.Build _ Y R (U' z) (sfinU z). - -Lemma letin'C z A : measurable A -> - letin' t - (letin' u' - (ret (measurable_fun_prod macc1of3' macc0of3'))) z A = - letin' u - (letin' t' - (ret (measurable_fun_prod macc0of3' macc1of3'))) z A. -Proof. -move=> mA. -rewrite !letin'E. +Lemma beta_prob_uniform {R : realType} : beta_prob 1 1 = uniform_prob (@ltr01 R). +Proof. +apply/funext => U. +rewrite /beta_prob /uniform_prob. +rewrite /mscale/= betafun11 invr1 !mul1e. +rewrite integral_XMonemX01 integral_uniform_pdf. under eq_integral. - move=> x _. - rewrite letin'E -uu'. - under eq_integral do rewrite retE /=. + move=> /= x. + rewrite inE => -[Ux/=]; rewrite in_itv/= => x10. + rewrite XMonemX01_11//=. over. -rewrite (sfinite_Fubini (T' z) (U' z) (fun x => \d_(x.1, x.2) A ))//; last first. - apply/measurable_EFinP => /=; rewrite (_ : (fun x => _) = mindic R mA)//. - by apply/funext => -[]. rewrite /=. -apply: eq_integral => y _. -by rewrite letin'E/= -tt'; apply: eq_integral => // x _; rewrite retE. +under [RHS]eq_integral. + move=> /= x. + rewrite inE => -[Ux/=]; rewrite in_itv/= => x10. + rewrite /uniform_pdf x10 subr0 invr1. + over. +by []. Qed. -End letin'C. -Arguments letin'C {d d1 d' X Y Z R} _ _ _ _. +Lemma beta_prob_integrable {R :realType} a b a' b' : + (beta_prob a b).-integrable `[0, 1] + (fun x : g_sigma_algebraType (R.-ocitv.-measurable) => (XMonemX a' b' x)%:E). +Proof. +apply/integrableP; split. + by apply/measurableT_comp => //; exact: measurable_fun_XMonemX. +apply: (@le_lt_trans _ _ (\int[beta_prob a b]_(x in `[0%R, 1%R]) 1)%E). + apply: ge0_le_integral => //=. + do 2 apply/measurableT_comp => //. + exact: measurable_fun_XMonemX. + move=> x; rewrite in_itv/= => /andP[x0 x1]. + rewrite lee_fin ger0_norm; last first. + by rewrite !mulr_ge0// exprn_ge0// onem_ge0. + by rewrite mulr_ile1// ?exprn_ge0 ?onem_ge0// exprn_ile1// ?onem_ge0// onem_le1. +rewrite integral_cst//= mul1e. +by rewrite -ge0_fin_numE// beta_prob_fin_num. +Qed. -Section letin'A. -Context d d' d1 d2 d3 (X : measurableType d) (Y : measurableType d') - (T1 : measurableType d1) (T2 : measurableType d2) (T3 : measurableType d3) - (R : realType). -Import Notations. -Variables (t : R.-sfker X ~> T1) - (u : R.-sfker T1 * X ~> T2) - (v : R.-sfker T2 * X ~> Y) - (v' : R.-sfker T2 * (T1 * X) ~> Y) - (vv' : forall y, v =1 fun xz => v' (xz.1, (y, xz.2))). - -Lemma letin'A x A : measurable A -> - letin' t (letin' u v') x A - = - (letin' (letin' t u) v) x A. -Proof. -move=> mA. -rewrite !letin'E. -under eq_integral do rewrite letin'E. -rewrite letin'_letin/=. -rewrite integral_kcomp; [|by []|]. - apply: eq_integral => z _. - apply: eq_integral => y _. - by rewrite (vv' z). -exact: measurableT_comp (@measurable_kernel _ _ _ _ _ v _ mA) _. -Qed. - -End letin'A. +Lemma beta_prob_integrable_onem {R : realType} a b a' b' : + (beta_prob a b).-integrable `[0, 1] + (fun x : g_sigma_algebraType (R.-ocitv.-measurable) => `1-(XMonemX a' b' x)%:E). +Proof. +apply: (eq_integrable _ (cst 1 \- (fun x : g_sigma_algebraType (R.-ocitv.-measurable) => + (XMonemX a' b' x)%:E))%E) => //. +apply: (@integrableB _ (g_sigma_algebraType R.-ocitv.-measurable)) => //=. + (* TODO: lemma? *) + apply/integrableP; split => //. + rewrite (eq_integral (fun x => (\1_setT x)%:E))/=; last first. + by move=> x _; rewrite /= indicT normr1. + rewrite integral_indic//= setTI /beta_prob /mscale/= lte_mul_pinfty//. + by rewrite lee_fin invr_ge0 betafun_ge0. + have /integrableP[_] := @integrable_XMonemX01 R a b. + under eq_integral. + move=> x _. + rewrite gee0_abs//; last first. + by rewrite lee_fin XMonemX01_ge0. + over. + by rewrite /= -/(betafunEFin a b) /= betafunEFinT. +exact: beta_prob_integrable. +Qed. + +Lemma beta_prob_integrable_dirac {R : realType} a b a' b' (c : bool) U : + (beta_prob a b).-integrable `[0, 1] + (fun x : g_sigma_algebraType (R.-ocitv.-measurable) => (XMonemX a' b' x)%:E * \d_c U)%E. +Proof. +apply: integrableMl => //=; last first. + exists 1; split => // x x1/= _ _; rewrite (le_trans _ (ltW x1))//. + by rewrite ger0_norm// indicE; case: (_ \in _). +exact: beta_prob_integrable. +Qed. + +Lemma beta_prob_integrable_onem_dirac {R : realType} a b a' b' (c : bool) U : + (beta_prob a b).-integrable `[0, 1] + (fun x : g_sigma_algebraType (R.-ocitv.-measurable) => `1-(XMonemX a' b' x)%:E * \d_c U)%E. +Proof. +apply: integrableMl => //=; last first. + exists 1; split => // x x1/= _ _; rewrite (le_trans _ (ltW x1))//. + by rewrite ger0_norm// indicE; case: (_ \in _). +exact: beta_prob_integrable_onem. +Qed. + +Local Close Scope ring_scope. + +Section integral_beta. +Context {R : realType}. +Local Notation mu := lebesgue_measure. + +Lemma beta_prob_dom a b : @beta_prob R a b `<< mu. +Proof. +move=> A mA muA0; rewrite /beta_prob /mscale/=. +apply/eqP; rewrite mule_eq0 eqe invr_eq0 gt_eqF/= ?betafun_gt0//; apply/eqP. +rewrite integral_XMonemX01; apply/eqP; rewrite eq_le; apply/andP; split; last first. + by apply: integral_ge0 => x _; rewrite lee_fin XMonemX01_ge0. +apply: (@le_trans _ _ (\int[mu]_(x in A `&` `[0%R, 1%R]%classic) 1)); last first. + rewrite integral_cst ?mul1e//=; last exact: measurableI. + by rewrite -[leRHS]muA0 measureIl. +apply: ge0_le_integral => //=; first exact: measurableI. +- by move=> x _; rewrite lee_fin XMonemX01_ge0. +- apply/measurable_funTS/measurableT_comp => //. + exact: measurable_XMonemX01. +- by move=> x _; rewrite lee_fin XMonemX01_le1. +Qed. + +Lemma integral_beta_prob a b f U : measurable U -> measurable_fun U f -> + \int[beta_prob a b]_(x in U) `|f x| < +oo -> + \int[beta_prob a b]_(x in U) f x = \int[mu]_(x in U) (f x * (beta_pdf a b x)%:E) :> \bar R. +Proof. +move=> mU mf finf. +rewrite -(Radon_Nikodym_change_of_variables (beta_prob_dom a b)) //=; last first. + by apply/integrableP; split. +apply: ae_eq_integral => //. +- apply: emeasurable_funM => //; apply: measurable_int. + apply: integrableS (Radon_Nikodym_integrable _) => //=. + exact: beta_prob_dom. +- apply: emeasurable_funM => //=; apply/measurableT_comp => //=. + by apply/measurable_funTS; exact: measurable_beta_pdf. +- apply: ae_eq_mul2l => /=. + rewrite Radon_NikodymE//=; first exact: beta_prob_dom. + move=> ?. + case: cid => /= h [h1 h2 h3]. + apply: integral_ae_eq => //. + + apply: integrableS h2 => //. (* integrableST? *) + apply/measurable_funTS/measurableT_comp => //. + exact: measurable_beta_pdf. + + by move=> E E01 mE; rewrite -h3//= integral_beta_pdf. +Qed. + +End integral_beta. + +Section beta_prob_bernoulliE. +Context {R : realType}. +Local Notation mu := lebesgue_measure. +Local Open Scope ring_scope. + +Definition div_betafun a b c d : R := betafun (a + c) (b + d) / betafun a b. + +Lemma div_betafun_ge0 a b c d : 0 <= div_betafun a b c d. +Proof. by rewrite /div_betafun divr_ge0// betafun_ge0. Qed. + +Lemma div_betafun_le1 a b c d : (0 < a)%N -> (0 < b)%N -> + div_betafun a b c d <= 1. +Proof. +move=> a0 b0. +rewrite /div_betafun ler_pdivrMr// ?mul1r ?betafun_gt0//. +rewrite !betafunE. +rewrite addn_eq0 (gtn_eqF a0)/=. +rewrite addn_eq0 (gtn_eqF b0)/=. +rewrite ler_pdivrMr ?ltr0n ?fact_gt0//. +rewrite mulrAC. +rewrite ler_pdivlMr ?ltr0n ?fact_gt0//. +rewrite -!natrM ler_nat. +case: a a0 => //. +move=> n. +rewrite addSn. +case: b b0 => //. +move=> m. +rewrite [(n + c).+1.-1]/=. +rewrite [n.+1.-1]/=. +rewrite [m.+1.-1]/=. +rewrite addnS. +rewrite [(_ + m).+1.-1]/=. +rewrite (addSn m d). +rewrite [(m + _).+1.-1]/=. +rewrite (addSn (n + c)). +rewrite [_.+1.-1]/=. +rewrite addSn addnS. +by rewrite leq_fact2// leq_addr. +Qed. + +Definition beta_prob_bernoulli a b c d U : \bar R := + \int[beta_prob a b]_(y in `[0, 1]) bernoulli (XMonemX01 c.+1 d.+1 y) U. + +Lemma beta_prob_bernoulliE a b c d U : (a > 0)%N -> (b > 0)%N -> + beta_prob_bernoulli a b c d U = bernoulli (div_betafun a b c d) U. +Proof. +move=> a0 b0. +rewrite /beta_prob_bernoulli. +under eq_integral => x. + rewrite inE/= in_itv/= => x01. + rewrite bernoulliE/= ?XMonemX01_ge0 ?XMonemX01_le1//. + over. +rewrite /=. +rewrite [in RHS]bernoulliE/= ?div_betafun_ge0 ?div_betafun_le1//=. +under eq_integral => x x01. + rewrite /XMonemX01 patchE x01/=. + over. +rewrite /=. +rewrite integralD//=; last 2 first. + exact: beta_prob_integrable_dirac. + exact: beta_prob_integrable_onem_dirac. +congr (_ + _). + rewrite integralZr//=; last exact: beta_prob_integrable. + congr (_ * _)%E. + rewrite integral_beta_prob//; last 2 first. + by apply/measurableT_comp => //; exact: measurable_fun_XMonemX. + by have /integrableP[_] := @beta_prob_integrable R a b c d. + rewrite /beta_pdf. + under eq_integral do rewrite EFinM -muleA muleC -muleA. + rewrite /=. + transitivity ((betafun a b)^-1%:E * + \int[mu]_(x in `[0%R, 1%R]) (XMonemX01 (a + c) (b + d) x)%:E : \bar R)%E. + rewrite -integralZl//=; last first. + apply/integrableP; split. + apply/measurable_EFinP/measurable_funTS. + exact: measurable_XMonemX01. + under eq_integral. + move=> x x01. + rewrite gee0_abs; last by rewrite lee_fin XMonemX01_ge0. + over. + by rewrite /= -betafunEFinT betafunEFin_lty. + apply: eq_integral => x x01. + (* TODO: lemma? property of XMonemX? *) + rewrite muleA muleC muleA -(EFinM (x ^+ c)). + rewrite -/(XMonemX c d x) -EFinM mulrA XMonemX_XMonemX01//. + by rewrite -EFinM mulrC. + by rewrite -betafunEFinT betafunEFinE -EFinM mulrC. +under eq_integral do rewrite muleC. +rewrite /=. +rewrite integralZl//=; last exact: beta_prob_integrable_onem. +rewrite muleC; congr (_ * _)%E. +rewrite integral_beta_prob//=; last 2 first. + apply/measurableT_comp => //=. + by apply/measurable_funB => //; exact: measurable_fun_XMonemX. + by have /integrableP[] := @beta_prob_integrable_onem R a b c d. +rewrite /beta_pdf. +under eq_integral do rewrite EFinM muleA. +rewrite integralZr//=; last first. + apply: integrableMr => //=. + - by apply/measurable_funB => //=; exact: measurable_fun_XMonemX. + - apply/ex_bound => //. + + apply: (@globally_properfilter _ _ 0%R) => //=. + by apply: inferP; rewrite in_itv/= lexx ler01. + + exists 1 => t. + rewrite /= in_itv/= => t01. + apply: normr_onem; apply/andP; split. + by rewrite mulr_ge0// exprn_ge0// ?onem_ge0//; case/andP: t01. + by rewrite mulr_ile1// ?exprn_ge0 ?exprn_ile1// ?onem_ge0 ?onem_le1//; case/andP: t01. + - exact: integrableS (integrable_XMonemX01 _ _). +transitivity ( + (\int[mu]_(x in `[0%R, 1%R]) + ((XMonemX01 a b x)%:E - (XMonemX01 (a + c) (b + d) x)%:E) : \bar R) + * (betafun a b)^-1%:E)%E. + congr (_ * _)%E. + apply: eq_integral => x x01. + rewrite /onem -EFinM mulrBl mul1r EFinB; congr (_ - _)%E. + by rewrite XMonemX_XMonemX01. +rewrite integralB_EFin//=; last 2 first. + exact: integrableS (integrable_XMonemX01 _ _). + exact: integrableS (integrable_XMonemX01 _ _). +rewrite -!betafunEFinT !betafunEFinE. +rewrite -EFinM. +rewrite mulrBl /onem mulfV; last by rewrite gt_eqF// betafun_gt0. +by []. +Qed. + +End beta_prob_bernoulliE. Declare Scope lang_scope. Delimit Scope lang_scope with P. @@ -294,7 +1402,7 @@ Definition mtyp t : measurableType (mtyp_disp t) := projT2 (measurable_of_typ t). Definition measurable_of_seq (l : seq typ) : {d & measurableType d} := - iter_mprod (map measurable_of_typ l). + iter_mprod (List.map measurable_of_typ l). End syntax_of_types. Arguments measurable_of_typ {R}. @@ -378,8 +1486,6 @@ match b with | binop_mult => Real end. -(* Import Notations. *) - Definition fun_of_binop g (b : binop) : (mctx g -> mtyp (type_of_binop b)) -> (mctx g -> mtyp (type_of_binop b)) -> @mctx R g -> @mtyp R (type_of_binop b) := match b with @@ -442,16 +1548,10 @@ Inductive exp : flag -> ctx -> typ -> Type := | exp_proj1 g t1 t2 : exp D g (Pair t1 t2) -> exp D g t1 | exp_proj2 g t1 t2 : exp D g (Pair t1 t2) -> exp D g t2 | exp_var g str t : t = lookup Unit g str -> exp D g t -| exp_bernoulli g (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : - exp D g (Prob Bool) -| exp_bernoulli_trunc g : - exp D g Real -> exp D g (Prob Bool) -| exp_binomial g (n : nat) (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : - exp D g (Prob Nat) -| exp_binomial_trunc g (n : nat) : - exp D g Real -> exp D g (Prob Nat) -| exp_uniform g (a b : R) (ab0 : (0 < b - a)%R) : exp D g (Prob Real) -| exp_beta g (a b : nat) (* NB: shound be R *) : exp D g (Prob Real) +| exp_bernoulli g : exp D g Real -> exp D g (Prob Bool) +| exp_binomial g (n : nat) : exp D g Real -> exp D g (Prob Nat) +| exp_uniform g (a b : R) (ab : (a < b)%R) : exp D g (Prob Real) +| exp_beta g (a b : nat) (* NB: should be R *) : exp D g (Prob Real) | exp_poisson g : nat -> exp D g Real -> exp D g Real | exp_normalize g t : exp P g t -> exp D g (Prob t) | exp_letin g t1 t2 str : exp P g t1 -> exp P ((str, t1) :: g) t2 -> @@ -483,12 +1583,10 @@ Arguments exp_bin {R} b {g} &. Arguments exp_rel {R} r {g} &. Arguments exp_pair {R g} & {t1 t2}. Arguments exp_var {R g} _ {t} & H. -Arguments exp_bernoulli {R g}. -Arguments exp_bernoulli_trunc {R g} &. -Arguments exp_binomial {R g}. +Arguments exp_bernoulli {R g} &. +Arguments exp_binomial {R g} &. Arguments exp_uniform {R g} &. Arguments exp_beta {R g} &. -Arguments exp_binomial_trunc {R g} &. Arguments exp_poisson {R g}. Arguments exp_normalize {R g _}. Arguments exp_letin {R g} & {_ _}. @@ -573,12 +1671,10 @@ Fixpoint free_vars k g t (e : @exp R k g t) : seq string := | exp_proj1 _ _ _ e => free_vars e | exp_proj2 _ _ _ e => free_vars e | exp_var _ x _ _ => [:: x] - | exp_bernoulli _ _ _ => [::] - | exp_bernoulli_trunc _ e => free_vars e - | exp_binomial _ _ _ _ => [::] + | exp_bernoulli _ e => free_vars e + | exp_binomial _ _ e => free_vars e | exp_uniform _ _ _ _ => [::] | exp_beta _ _ _ => [::] - | exp_binomial_trunc _ _ e => free_vars e | exp_poisson _ _ e => free_vars e | exp_normalize _ _ e => free_vars e | exp_letin _ _ _ x e1 e2 => free_vars e1 ++ rem x (free_vars e2) @@ -594,9 +1690,11 @@ End free_vars. Definition dval R g t := @mctx R g -> @mtyp R t. Definition pval R g t := R.-sfker @mctx R g ~> @mtyp R t. + Section weak. Context {R : realType}. Implicit Types (g h : ctx) (x : string * typ). +Local Open Scope ring_scope. Fixpoint mctx_strong g h x (f : @mctx R (g ++ x :: h)) : @mctx R (g ++ h) := match g as g0 return mctx (g0 ++ x :: h) -> mctx (g0 ++ h) with @@ -684,18 +1782,6 @@ Implicit Type (g : ctx) (str : string). Local Open Scope lang_scope. Local Open Scope ring_scope. -Definition bernoulli0 := @bernoulli R 0%R%:nng ler01. - -HB.instance Definition _ := Probability.on bernoulli0. - -Lemma __ : Measurable.sort - (pprobability - [the measurableType (R.-ocitv.-measurable).-sigma of - g_sigma_algebraType (R.-ocitv.-measurable)] R) = -Measurable.sort (@mtyp R (Prob Real)). -rewrite /=. -(* done. *) -Abort. Inductive evalD : forall g t, exp D g t -> forall f : dval R g t, measurable_fun setT f -> Prop := @@ -707,7 +1793,7 @@ Inductive evalD : forall g t, exp D g t -> | eval_real g r : ([r:R] : exp D g _) -D> cst r ; kr r -| eval_pow g n (e : exp D g _) f mf : e -D> f ; mf -> +| eval_pow g n (e : exp D g _) f mf : e -D> f ; mf -> [e ^+ {n}] -D> (fun x => f x ^+ n) ; (measurable_funX n mf) | eval_bin g bop (e1 : exp D g _) f1 mf1 e2 f2 mf2 : @@ -736,35 +1822,25 @@ Inductive evalD : forall g t, exp D g t -> | eval_var g x H : let i := index x (dom g) in exp_var x H -D> acc_typ (map snd g) i ; measurable_acc_typ (map snd g) i -| eval_bernoulli g r r1 : - (exp_bernoulli r r1 : exp D g _) -D> cst (bernoulli r1) ; - measurable_cst _ - -| eval_bernoulli_trunc g e r mr : - e -D> r ; mr -> - (exp_bernoulli_trunc e : exp D g _) -D> bernoulli_trunc \o r ; - measurableT_comp measurable_bernoulli_trunc mr - -| eval_binomial g n (p : {nonneg R}) (p1 : (p%:num <= 1)%R) : - (exp_binomial n p p1 : exp D g _) -D> cst (binomial_probability n p1) ; - measurable_cst _ +| eval_bernoulli g e r mr : + e -D> r ; mr -> (exp_bernoulli e : exp D g _) -D> bernoulli \o r ; + measurableT_comp measurable_bernoulli mr -| eval_binomial_trunc g n e r mr : - e -D> r ; mr -> - (exp_binomial_trunc n e : exp D g _) -D> binomial_probability_trunc n \o r ; - measurableT_comp (measurable_binomial_probability_trunc n) mr +| eval_binomial g n e r mr : + e -D> r ; mr -> (exp_binomial n e : exp D g _) -D> binomial_prob n \o r ; + measurableT_comp (measurable_binomial_prob n) mr -| eval_uniform g (a b : R) (ab0 : (0 < b - a)%R) : - (exp_uniform a b ab0 : exp D g _) -D> cst (uniform_probability ab0) ; - measurable_cst _ +| eval_uniform g (a b : R) (ab : (a < b)%R) : + (exp_uniform a b ab : exp D g _) -D> cst (uniform_prob ab) ; + measurable_cst _ | eval_beta g (a b : nat) : - (exp_beta a b : exp D g _) -D> cst (beta_nat a b) ; measurable_cst _ + (exp_beta a b : exp D g _) -D> cst (beta_prob a b) ; measurable_cst _ | eval_poisson g n (e : exp D g _) f mf : e -D> f ; mf -> - exp_poisson n e -D> poisson n \o f ; - measurableT_comp (measurable_poisson n) mf + exp_poisson n e -D> poisson_pdf n \o f ; + measurableT_comp (measurable_poisson_pdf n) mf | eval_normalize g t (e : exp P g t) k : e -P> k -> @@ -885,29 +1961,20 @@ all: (rewrite {g t e u v mu mv hu}). inversion 1; subst g0. inj_ex H9; rewrite -H9. by inj_ex H10. -- move=> g r r1 {}v {}mv. - inversion 1; subst g0 r0. - inj_ex H3; subst v. - by have -> : r1 = r3 by []. - move=> g e r mr ev IH {}v {}mv. inversion 1; subst g0. inj_ex H0; subst e0. - inj_ex H4; subst v. - by rewrite (IH _ _ H2). -- move=> g n p p1 {}v {}mv. - inversion 1; subst g0 n0 p0. - inj_ex H4; subst v. - by have -> : p1 = p3 by []. + inj_ex H3; subst v. + by rewrite (IH _ _ H4). - move=> g n e f mf ev IH {}v {}mv. inversion 1; subst g0 n0. inj_ex H2; subst e0. inj_ex H4; subst v. - inj_ex H5; subst mv. - by rewrite (IH _ _ H3). -- move=> g a b ab0 {}v {}mv. + by rewrite (IH _ _ H5). +- move=> g a b ab {}v {}mv. inversion 1; subst g0 a0 b0. inj_ex H4; subst v. - by have -> : ab0 = ab2. + by have -> : ab = ab1. - (* TODO: beta *) move=> g a b {}v {}mv. inversion 1; subst g0 a0 b0. by inj_ex H4; subst v. @@ -1043,29 +2110,20 @@ all: rewrite {g t e u v eu}. inversion 1; subst g0. inj_ex H9; rewrite -H9. by inj_ex H10. -- move=> g r r1 {}v {}mv. - inversion 1; subst g0 r0. - inj_ex H3; subst v. - by have -> : r1 = r3 by []. - move=> g e r mr ev IH {}v {}mv. inversion 1; subst g0. inj_ex H0; subst e0. - inj_ex H4; subst v. - by rewrite (IH _ _ H2). -- move=> g n p p1 {}v {}mv. - inversion 1; subst g0 n0 p0. - inj_ex H4; subst v. - by have -> : p1 = p3 by []. + inj_ex H3; subst v. + by rewrite (IH _ _ H4). - move=> g n e f mf ev IH {}v {}mv. inversion 1; subst g0 n0. inj_ex H2; subst e0. inj_ex H4; subst v. - inj_ex H5; subst mv. - by rewrite (IH _ _ H3). -- move=> g a b ab0 {}v {}mv. + by rewrite (IH _ _ H5). +- move=> g a b ab {}v {}mv. inversion 1; subst g0 a0 b0. inj_ex H4; subst v. - by have -> : ab0 = ab2. + by have -> : ab = ab1. - (* TODO: beta case*) move=> g a b {}v {}mv. inversion 1; subst g0 a0 b0. by inj_ex H4; subst v. @@ -1162,17 +2220,16 @@ all: rewrite {z g t}. - move=> g t1 t2 e [f [mf H]]. by exists (snd \o f); eexists; exact: eval_proj2. - by move=> g x t tE; subst t; eexists; eexists; exact: eval_var. -- by move=> r r1; eexists; eexists; exact: eval_bernoulli. - move=> g e [p [mp H]]. - by exists (bernoulli_trunc \o p); eexists; exact: eval_bernoulli_trunc. -- by move=> p p1; eexists; eexists; exact: eval_binomial. + exists ((bernoulli : R -> pprobability bool R) \o p). + by eexists; exact: eval_bernoulli. - move=> g n e [p [mp H]]. - exists (binomial_probability_trunc n \o p). - eexists; exact: (eval_binomial_trunc n). + exists ((binomial_prob n : R -> pprobability nat R) \o p). + by eexists; exact: (eval_binomial n). - by eexists; eexists; exact: eval_uniform. - by eexists; eexists; exact: eval_beta. - move=> g h e [f [mf H]]. - by exists (poisson h \o f); eexists; exact: eval_poisson. + by exists (poisson_pdf h \o f); eexists; exact: eval_poisson. - move=> g t e [k ek]. by exists (normalize_pt k); eexists; exact: eval_normalize. - move=> g t1 t2 x e1 [k1 ev1] e2 [k2 ev2]. @@ -1340,35 +2397,26 @@ Lemma execD_var g x (H : nth Unit (map snd g) (index x (dom g)) = lookup Unit g (measurable_acc_typ (map snd g) i). Proof. by move=> i; apply/execD_evalD; exact: eval_var. Qed. -Lemma execD_bernoulli g r (r1 : (r%:num <= 1)%R) : - @execD g _ (exp_bernoulli r r1) = - existT _ (cst [the probability _ _ of bernoulli r1]) (measurable_cst _). -Proof. exact/execD_evalD/eval_bernoulli. Qed. - -Lemma execD_bernoulli_trunc g e : - @execD g _ (exp_bernoulli_trunc e) = - existT _ (bernoulli_trunc \o projT1 (execD e)) (measurableT_comp measurable_bernoulli_trunc (projT2 (execD e))). -Proof. exact/execD_evalD/eval_bernoulli_trunc/evalD_execD. Qed. - -Lemma execD_binomial g n p (p1 : (p%:num <= 1)%R) : - @execD g _ (exp_binomial n p p1) = - existT _ (cst [the probability _ _ of binomial_probability n p1]) (measurable_cst _). -Proof. exact/execD_evalD/eval_binomial. Qed. +Lemma execD_bernoulli g e : + @execD g _ (exp_bernoulli e) = + existT _ ((bernoulli : R -> pprobability bool R) \o projT1 (execD e)) + (measurableT_comp measurable_bernoulli (projT2 (execD e))). +Proof. exact/execD_evalD/eval_bernoulli/evalD_execD. Qed. -Lemma execD_binomial_trunc g n e : - @execD g _ (exp_binomial_trunc n e) = - existT _ (binomial_probability_trunc n \o projT1 (execD e)) - (measurableT_comp (measurable_binomial_probability_trunc n) (projT2 (execD e))). -Proof. exact/execD_evalD/eval_binomial_trunc/evalD_execD. Qed. +Lemma execD_binomial g n e : + @execD g _ (exp_binomial n e) = + existT _ ((binomial_prob n : R -> pprobability nat R) \o projT1 (execD e)) + (measurableT_comp (measurable_binomial_prob n) (projT2 (execD e))). +Proof. exact/execD_evalD/eval_binomial/evalD_execD. Qed. Lemma execD_uniform g a b ab0 : @execD g _ (exp_uniform a b ab0) = - existT _ (cst [the probability _ _ of uniform_probability ab0]) (measurable_cst _). + existT _ (cst [the probability _ _ of uniform_prob ab0]) (measurable_cst _). Proof. exact/execD_evalD/eval_uniform. Qed. -Lemma execD_beta_nat g a b : +Lemma execD_beta g a b : @execD g _ (exp_beta a b) = - existT _ (cst [the probability _ _ of beta_nat a b]) (measurable_cst _). + existT _ (cst [the probability _ _ of beta_prob a b]) (measurable_cst _). Proof. exact/execD_evalD/eval_beta. Qed. Lemma execD_normalize_pt g t (e : exp P g t) : @@ -1379,8 +2427,8 @@ Proof. exact/execD_evalD/eval_normalize/evalP_execP. Qed. Lemma execD_poisson g n (e : exp D g Real) : execD (exp_poisson n e) = - existT _ (poisson n \o (projT1 (execD e))) - (measurableT_comp (measurable_poisson n) (projT2 (execD e))). + existT _ (poisson_pdf n \o projT1 (execD e)) + (measurableT_comp (measurable_poisson_pdf n) (projT2 (execD e))). Proof. exact/execD_evalD/eval_poisson/evalD_execD. Qed. Lemma execP_if g st e1 e2 e3 : @@ -1416,3 +2464,32 @@ End execution_functions. Arguments execD_var_erefl {R g} str. Arguments execP_weak {R} g h x {t} e. Arguments exp_var'E {R} str. + +Local Open Scope lang_scope. +Lemma congr_letinl {R : realType} g t1 t2 str (e1 e2 : @exp _ _ g t1) + (e : @exp _ _ (_ :: g) t2) x U : + (forall y V, execP e1 y V = execP e2 y V) -> + measurable U -> + @execP R g t2 [let str := e1 in e] x U = + @execP R g t2 [let str := e2 in e] x U. +Proof. by move=> + mU; move/eq_sfkernel => He; rewrite !execP_letin He. Qed. + +Lemma congr_letinr {R : realType} g t1 t2 str (e : @exp _ _ _ t1) + (e1 e2 : @exp _ _ (_ :: g) t2) x U : + (forall y V, execP e1 (y, x) V = execP e2 (y, x) V) -> + @execP R g t2 [let str := e in e1] x U = @execP R g t2 [let str := e in e2] x U. +Proof. +by move=> He; rewrite !execP_letin !letin'E; apply: eq_integral => ? _; exact: He. +Qed. + +Lemma congr_normalize {R : realType} g t (e1 e2 : @exp R _ g t) : + (forall x U, execP e1 x U = execP e2 x U) -> + execD [Normalize e1] = execD [Normalize e2]. +Proof. +move=> He; apply: eq_execD. +rewrite !execD_normalize_pt /=. +f_equal. +apply: eq_kernel => y V. +exact: He. +Qed. +Local Close Scope lang_scope. diff --git a/theories/lang_syntax_examples.v b/theories/lang_syntax_examples.v index c52cc2748..95920323e 100644 --- a/theories/lang_syntax_examples.v +++ b/theories/lang_syntax_examples.v @@ -1,34 +1,46 @@ Require Import String. From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. -From mathcomp.classical Require Import mathcomp_extra boolp classical_sets. +From mathcomp.classical Require Import mathcomp_extra boolp. +From mathcomp Require Import classical_sets. From mathcomp.classical Require Import functions cardinality fsbigop. From mathcomp Require Import signed reals ereal topology normedtype sequences. From mathcomp Require Import esum measure lebesgue_measure numfun. -From mathcomp Require Import lebesgue_integral kernel prob_lang. +From mathcomp Require Import lebesgue_integral probability kernel prob_lang. From mathcomp Require Import lang_syntax_util lang_syntax. From mathcomp Require Import ring lra. -(******************************************************************************) -(* Examples using the Probabilistic Programming Language of lang_syntax.v *) +(**md**************************************************************************) +(* # Examples using the Probabilistic Programming Language of lang_syntax.v *) (* *) -(* sample_pair_syntax := normalize ( *) +(* sample_pair1213 := normalize ( *) (* let x := sample (bernoulli 1/2) in *) (* let y := sample (bernoulli 1/3) in *) (* return (x, y)) *) (* *) +(* sample_and1213 := normalize ( *) +(* let x := sample (bernoulli 1/2) in *) +(* let y := sample (bernoulli 1/3) in *) +(* return (x && y)) *) +(* *) (* bernoulli13_score := normalize ( *) (* let x := sample (bernoulli 1/3) in *) (* let _ := if x then score (1/3) else score (2/3) in *) (* return x) *) (* *) -(* bernoulli12_score := normalize ( *) -(* let x := sample (bernoulli 1/2) in *) -(* let _ := if x then score (1/3) else score (2/3) in *) -(* return x) *) +(* sample_binomial3 := *) +(* let x := sample (binomial 3 1/2) in *) +(* return x *) (* *) (* hard_constraint := let x := Score {0}:R in return TT *) (* *) +(* guard := *) +(* let p := sample (bernoulli (1 / 3)) in *) +(* let _ := if p then return TT else score 0 in *) +(* return p *) +(* *) +(* more examples about uniform, beta, and bernoulli distributions *) +(* *) (* associativity of let-in expressions *) (* *) (* staton_bus_syntax == example from [Staton, ESOP 2017] *) @@ -51,90 +63,8 @@ Local Open Scope classical_set_scope. Local Open Scope ring_scope. Local Open Scope ereal_scope. -(* letin' versions of rewriting laws *) -Lemma letin'_sample_bernoulli d d' (T : measurableType d) - (T' : measurableType d') (R : realType)(r : {nonneg R}) (r1 : (r%:num <= 1)%R) - (u : R.-sfker bool * T ~> T') x y : - letin' (sample_cst (bernoulli r1)) u x y = - r%:num%:E * u (true, x) y + (`1- (r%:num))%:E * u (false, x) y. -Proof. -rewrite letin'E/=. -rewrite ge0_integral_measure_sum// 2!big_ord_recl/= big_ord0 adde0/=. -by rewrite !ge0_integral_mscale//= !integral_dirac//= !diracT 2!mul1e. -Qed. - -Section letin'_return. -Context d d' d3 (X : measurableType d) (Y : measurableType d') - (Z : measurableType d3) (R : realType). - -Lemma letin'_kret (k : R.-sfker X ~> Y) - (f : Y * X -> Z) (mf : measurable_fun setT f) x U : - measurable U -> - letin' k (ret mf) x U = k x (curry f ^~ x @^-1` U). -Proof. -move=> mU; rewrite letin'E. -under eq_integral do rewrite retE. -rewrite integral_indic ?setIT// -[X in measurable X]setTI. -exact: (measurableT_comp mf). -Qed. - -Lemma letin'_retk (f : X -> Y) (mf : measurable_fun setT f) - (k : R.-sfker Y * X ~> Z) x U : - measurable U -> letin' (ret mf) k x U = k (f x, x) U. -Proof. -move=> mU; rewrite letin'E retE integral_dirac ?diracT ?mul1e//. -exact: (measurableT_comp (measurable_kernel k _ mU)). -Qed. - -End letin'_return. - -Section letin'_ite. -Context d d2 d3 (T : measurableType d) (T2 : measurableType d2) - (Z : measurableType d3) (R : realType). -Variables (k1 k2 : R.-sfker T ~> Z) - (u : R.-sfker Z * T ~> T2) - (f : T -> bool) (mf : measurable_fun setT f) - (t : T) (U : set T2). - -Lemma letin'_iteT : f t -> letin' (ite mf k1 k2) u t U = letin' k1 u t U. -Proof. -move=> ftT. -rewrite !letin'E/=. -apply: eq_measure_integral => V mV _. -by rewrite iteE ftT. -Qed. - -Lemma letin'_iteF : ~~ f t -> letin' (ite mf k1 k2) u t U = letin' k2 u t U. -Proof. -move=> ftF. -rewrite !letin'E/=. -apply: eq_measure_integral => V mV _. -by rewrite iteE (negbTE ftF). -Qed. - -End letin'_ite. -(* /letin' versions of rewriting laws *) - Local Open Scope lang_scope. -Lemma execP_letinL {R : realType} g t1 t2 x (e1 : @exp R P g t1) (e1' : exp P g t1) - (e2 : exp P ((x, t1) :: g) t2) : - forall U, measurable U -> - execP e1 = execP e1' -> - execP [let x := e1 in e2] ^~ U = execP [let x := e1' in e2] ^~ U. -Proof. -by move=> U mU e1e1'; rewrite !execP_letin e1e1'. -Qed. - -Lemma execP_letinR {R : realType} g t1 t2 x (e1 : @exp R P g t1) - (e2 : exp P _ t2) (e2' : exp P ((x, t1) :: g) t2) : - forall U, measurable U -> - execP e2 = execP e2' -> - execP [let x := e1 in e2] ^~ U = execP [let x := e1 in e2'] ^~ U. -Proof. -by move=> U mU e1e1'; rewrite !execP_letin e1e1'. -Qed. - Local Close Scope lang_scope. (* simple tests to check bidirectional hints *) @@ -187,9 +117,9 @@ Import Notations. Context {R : realType}. Lemma exec_normalize_return g x r : - projT1 (@execD _ g _ [Normalize return r:R]) x = + projT1 (@execD _ g _ [Normalize return r:R]) x = @dirac _ (measurableTypeR R) r _ :> probability _ R. - (* TODO: try to use the notation \d_r *) + (* NB: \d_r notation? *) Proof. by rewrite execD_normalize_pt execP_return execD_real//=; exact: normalize_kdirac. Qed. @@ -202,16 +132,15 @@ Local Open Scope ring_scope. Import Notations. Context {R : realType}. -Definition sample_pair_syntax0 : @exp R _ [::] _ := - [let "x" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in - let "y" := Sample {exp_bernoulli (1 / 3%:R)%:nng (p1S 2)} in +Definition sample_pair1213' : @exp R _ [::] _ := + [let "x" := Sample {exp_bernoulli [{1 / 2}:R]} in + let "y" := Sample {exp_bernoulli [{1 / 3}:R]} in return (#{"x"}, #{"y"})]. -Definition sample_pair_syntax : exp _ [::] _ := - [Normalize {sample_pair_syntax0}]. +Definition sample_pair1213 : exp _ [::] _ := [Normalize {sample_pair1213'}]. -Lemma exec_sample_pair0 (A : set (bool * bool)) : - @execP R [::] _ sample_pair_syntax0 tt A = +Lemma exec_sample_pair1213' (A : set (bool * bool)) : + @execP R [::] _ sample_pair1213' tt A = ((1 / 2)%:E * ((1 / 3)%:E * \d_(true, true) A + (1 - 1 / 3)%:E * \d_(true, false) A) + @@ -219,110 +148,116 @@ Lemma exec_sample_pair0 (A : set (bool * bool)) : ((1 / 3)%:E * \d_(false, true) A + (1 - 1 / 3)%:E * \d_(false, false) A))%E. Proof. -rewrite !execP_letin !execP_sample !execD_bernoulli execP_return /=. +rewrite !execP_letin !execP_sample !execD_bernoulli !execP_return /=. rewrite execD_pair !exp_var'E (execD_var_erefl "x") (execD_var_erefl "y") /=. -rewrite letin'E ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !diracT !mul1e. -rewrite !letin'E !ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. -by rewrite !integral_dirac//= !diracT !mul1e. +rewrite !execD_real//=. +do 2 (rewrite letin'E/= integral_bernoulli//=; last lra). +by rewrite letin'E/= integral_bernoulli//=; lra. Qed. -Lemma exec_sample_pair0_TandT : - @execP R [::] _ sample_pair_syntax0 tt [set (true, true)] = (1 / 6)%:E. +Lemma exec_sample_pair1213'_TandT : + @execP R [::] _ sample_pair1213' tt [set (true, true)] = (1 / 6)%:E. Proof. -rewrite exec_sample_pair0 !diracE mem_set//; do 3 rewrite memNset//=. +rewrite exec_sample_pair1213' !diracE mem_set//; do 3 rewrite memNset//=. by rewrite /= !mule0 mule1 !add0e mule0 adde0; congr (_%:E); lra. Qed. -Lemma exec_sample_pair0_TandF : - @execP R [::] _ sample_pair_syntax0 tt [set (true, false)] = (1 / 3)%:E. +Lemma exec_sample_pair1213'_TandT' : + @execP R [::] _ sample_pair1213' tt [set p | p.1 && p.2] = (1 / 6)%:E. Proof. -rewrite exec_sample_pair0 !diracE memNset// mem_set//; do 2 rewrite memNset//. +rewrite exec_sample_pair1213' !diracE mem_set//; do 3 rewrite memNset//=. by rewrite /= !mule0 mule1 !add0e mule0 adde0; congr (_%:E); lra. Qed. -Lemma exec_sample_pair0_TandT' : - @execP R [::] _ sample_pair_syntax0 tt [set p | p.1 && p.2] = (1 / 6)%:E. +Lemma exec_sample_pair1213'_TandF : + @execP R [::] _ sample_pair1213' tt [set (true, false)] = (1 / 3)%:E. Proof. -rewrite exec_sample_pair0 !diracE mem_set//; do 3 rewrite memNset//=. +rewrite exec_sample_pair1213' !diracE memNset// mem_set//; do 2 rewrite memNset//. by rewrite /= !mule0 mule1 !add0e mule0 adde0; congr (_%:E); lra. Qed. -Lemma exec_sample_pair_TorT : - (projT1 (execD sample_pair_syntax)) tt [set p | p.1 || p.2] = (2 / 3)%:E. +Lemma exec_sample_pair1213_TorT : + (projT1 (execD sample_pair1213)) tt [set p | p.1 || p.2] = (2 / 3)%:E. Proof. -rewrite execD_normalize_pt normalizeE/= exec_sample_pair0. +rewrite execD_normalize_pt normalizeE/= exec_sample_pair1213'. rewrite !diracE; do 4 rewrite mem_set//=. rewrite eqe ifF; last by apply/negbTE/negP => /orP[/eqP|//]; lra. -rewrite exec_sample_pair0 !diracE; do 3 rewrite mem_set//; rewrite memNset//=. +rewrite exec_sample_pair1213' !diracE; do 3 rewrite mem_set//; rewrite memNset//=. by rewrite !mule1; congr (_%:E); field. Qed. -Definition sample_and_syntax0 : @exp R _ [::] _ := - [let "x" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in - let "y" := Sample {exp_bernoulli (1 / 3%:R)%:nng (p1S 2)} in +End sample_pair. + +Section sample_and. +Local Open Scope lang_scope. +Local Open Scope ring_scope. +Import Notations. +Context {R : realType}. + +Definition sample_and1213' : @exp R _ [::] _ := + [let "x" := Sample {exp_bernoulli [{1 / 2}:R]} in + let "y" := Sample {exp_bernoulli [{1 / 3}:R]} in return #{"x"} && #{"y"}]. -Lemma exec_sample_and0 (A : set bool) : - @execP R [::] _ sample_and_syntax0 tt A = ((1 / 6)%:E * \d_true A + - (1 - 1 / 6)%:E * \d_false A)%E. -Proof. -rewrite !execP_letin !execP_sample !execD_bernoulli execP_return /=. -rewrite (@execD_bin _ _ binop_and) !exp_var'E (execD_var_erefl "x") (execD_var_erefl "y") /=. -rewrite letin'E ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !diracT !mul1e. -rewrite !letin'E !ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !diracT !mul1e. +Lemma exec_sample_and1213' (A : set bool) : + @execP R [::] _ sample_and1213' tt A = ((1 / 6)%:E * \d_true A + + (1 - 1 / 6)%:E * \d_false A)%E. +Proof. +rewrite !execP_letin !execP_sample/= !execD_bernoulli execP_return /=. +rewrite !(@execD_bin _ _ binop_and) !exp_var'E (execD_var_erefl "x"). +rewrite (execD_var_erefl "y") /= !letin'E/= !execD_real/=. +rewrite integral_bernoulli//=; last lra. +rewrite !letin'E/= integral_bernoulli//=; last lra. +rewrite integral_bernoulli//=; last lra. +rewrite /onem. rewrite muleDr// -addeA; congr (_ + _)%E. by rewrite !muleA; congr (_%:E); congr (_ * _); field. rewrite -muleDl// !muleA -muleDl//. by congr (_%:E); congr (_ * _); field. Qed. -Definition sample_bernoulli_and3 : @exp R _ [::] _ := - [let "x" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in - let "y" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in - let "z" := Sample {exp_bernoulli (1 / 2)%:nng (p1S 1)} in +Definition sample_and121212 : @exp R _ [::] _ := + [let "x" := Sample {exp_bernoulli [{1 / 2}:R]} in + let "y" := Sample {exp_bernoulli [{1 / 2}:R]} in + let "z" := Sample {exp_bernoulli [{1 / 2}:R]} in return #{"x"} && #{"y"} && #{"z"}]. -Lemma exec_sample_bernoulli_and3 t U : - execP sample_bernoulli_and3 t U = ((1 / 8)%:E * \d_true U + - (1 - 1 / 8)%:E * \d_false U)%E. -Proof. -rewrite !execP_letin !execP_sample !execD_bernoulli execP_return /=. -rewrite !(@execD_bin _ _ binop_and) !exp_var'E. -rewrite (execD_var_erefl "x") (execD_var_erefl "y") (execD_var_erefl "z") /=. -rewrite letin'E ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !diracT !mul1e. -rewrite !letin'E !ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !diracT !mul1e. -rewrite !letin'E !ge0_integral_measure_add//= !ge0_integral_mscale//= /onem. -rewrite !integral_dirac//= !diracT !mul1e. -rewrite !muleDr// -!addeA. -by congr (_ + _)%E; rewrite ?addeA !muleA -?muleDl//; -congr (_ * _)%E; congr (_%:E); field. +Lemma exec_sample_and121212 t U : + execP sample_and121212 t U = ((1 / 8)%:E * \d_true U + + (1 - 1 / 8)%:E * \d_false U)%E. +Proof. +rewrite !execP_letin !execP_sample !execD_bernoulli !execP_return /=. +rewrite !(@execD_bin _ _ binop_and) !exp_var'E (execD_var_erefl "x"). +rewrite (execD_var_erefl "y") (execD_var_erefl "z") /= !execD_real/=. +do 3 (rewrite !letin'E/= integral_bernoulli//=; last lra). +do 2 (rewrite integral_bernoulli//=; last lra). +rewrite !letin'E/= integral_bernoulli//=; last lra. +rewrite !muleDr// -!addeA; congr (_ + _)%E. + by rewrite !muleA; congr *%E; congr EFin; field. +rewrite !muleA -!muleDl//; congr *%E; congr EFin. +by rewrite /onem; field. Qed. -End sample_pair. +End sample_and. -Section bernoulli_examples. +Section sample_score. Local Open Scope ring_scope. Local Open Scope lang_scope. Import Notations. Context {R : realType}. Definition bernoulli13_score := [Normalize - let "x" := Sample {@exp_bernoulli R [::] (1 / 3%:R)%:nng (p1S 2)} in - let "_" := if #{"x"} then Score {(1 / 3)}:R else Score {(2 / 3)}:R in + let "x" := Sample {@exp_bernoulli R [::] [{1 / 3}:R]} in + let "_" := if #{"x"} then Score {1 / 3}:R else Score {2 / 3}:R in return #{"x"}]. Lemma exec_bernoulli13_score : - execD bernoulli13_score = execD (exp_bernoulli (1 / 5%:R)%:nng (p1S 4)). + execD bernoulli13_score = execD (exp_bernoulli [{1 / 5}:R]). Proof. apply: eq_execD. rewrite execD_bernoulli/= /bernoulli13_score execD_normalize_pt 2!execP_letin. rewrite execP_sample/= execD_bernoulli/= execP_if /= exp_var'E. -rewrite (execD_var_erefl "x")/= !execP_return/= 2!execP_score 2!execD_real/=. +rewrite (execD_var_erefl "x")/= !execP_return/= 2!execP_score !execD_real/=. apply: funext=> g; apply: eq_probability => U. rewrite normalizeE !letin'E/=. under eq_integral. @@ -330,41 +265,40 @@ under eq_integral. rewrite !letin'E. under eq_integral do rewrite retE /=. over. -rewrite !ge0_integral_measure_add //=; last by move=> b _; rewrite integral_ge0. -rewrite !ge0_integral_mscale //=; last 2 first. - by move=> b _; rewrite integral_ge0. - by move=> b _; rewrite integral_ge0. -rewrite !integral_dirac// !diracT !mul1e. +rewrite /=. +rewrite integral_bernoulli//=; [|lra|by move=> b; rewrite integral_ge0]. rewrite iteE/= !ge0_integral_mscale//=. rewrite ger0_norm//. rewrite !integral_indic//= !iteE/= /mscale/=. rewrite setTI !diracT !mule1. rewrite ger0_norm//. rewrite -EFinD/= eqe ifF; last first. - apply/negbTE/negP => /orP[/eqP|//]; rewrite /onem. lra. + by apply/negbTE/negP => /orP[/eqP|//]; rewrite /onem; lra. +rewrite integral_bernoulli//=; last lra. rewrite !letin'E/= !iteE/=. rewrite !ge0_integral_mscale//=. rewrite ger0_norm//. rewrite !integral_dirac//= !diracT !mul1e ger0_norm//. rewrite exp_var'E (execD_var_erefl "x")/=. -rewrite /bernoulli/= measure_addE/= /mscale/= !mul1r. +rewrite !indicT/= !mulr1. +rewrite bernoulliE//=; last lra. by rewrite muleDl//; congr (_ + _)%E; rewrite -!EFinM; congr (_%:E); - rewrite !indicT !indicE /onem /=; case: (_ \in _); field. + rewrite !indicE /onem /=; case: (_ \in _); field. Qed. Definition bernoulli12_score := [Normalize - let "x" := Sample {@exp_bernoulli R [::] (1 / 2)%:nng (p1S 1)} in - let "r" := if #{"x"} then Score {(1 / 3)}:R else Score {(2 / 3)}:R in + let "x" := Sample {@exp_bernoulli R [::] [{1 / 2}:R]} in + let "r" := if #{"x"} then Score {1 / 3}:R else Score {2 / 3}:R in return #{"x"}]. Lemma exec_bernoulli12_score : - execD bernoulli12_score = execD (exp_bernoulli (1 / 3%:R)%:nng (p1S 2)). + execD bernoulli12_score = execD (exp_bernoulli [{1 / 3}:R]). Proof. apply: eq_execD. rewrite execD_bernoulli/= /bernoulli12_score execD_normalize_pt 2!execP_letin. rewrite execP_sample/= execD_bernoulli/= execP_if /= exp_var'E. -rewrite (execD_var_erefl "x")/= !execP_return/= 2!execP_score 2!execD_real/=. +rewrite (execD_var_erefl "x")/= !execP_return/= 2!execP_score !execD_real/=. apply: funext=> g; apply: eq_probability => U. rewrite normalizeE !letin'E/=. under eq_integral. @@ -372,11 +306,7 @@ under eq_integral. rewrite !letin'E. under eq_integral do rewrite retE /=. over. -rewrite !ge0_integral_measure_add //=; last by move=> b _; rewrite integral_ge0. -rewrite !ge0_integral_mscale //=; last 2 first. - by move=> b _; rewrite integral_ge0. - by move=> b _; rewrite integral_ge0. -rewrite !integral_dirac// !diracT !mul1e. +rewrite /= integral_bernoulli//=; [|lra|by move=> b; rewrite integral_ge0]. rewrite iteE/= !ge0_integral_mscale//=. rewrite ger0_norm//. rewrite !integral_indic//= !iteE/= /mscale/=. @@ -385,12 +315,14 @@ rewrite ger0_norm//. rewrite -EFinD/= eqe ifF; last first. apply/negbTE/negP => /orP[/eqP|//]. by rewrite /onem; lra. +rewrite integral_bernoulli//=; last lra. rewrite !letin'E/= !iteE/=. rewrite !ge0_integral_mscale//=. rewrite ger0_norm//. rewrite !integral_dirac//= !diracT !mul1e ger0_norm//. rewrite exp_var'E (execD_var_erefl "x")/=. -rewrite /bernoulli/= measure_addE/= /mscale/= !mul1r. +rewrite bernoulliE//=; last lra. +rewrite !mul1r. rewrite muleDl//; congr (_ + _)%E; rewrite -!EFinM; congr (_%:E); @@ -399,15 +331,12 @@ Qed. (* https://dl.acm.org/doi/pdf/10.1145/2933575.2935313 (Sect. 4) *) Definition bernoulli14_score := [Normalize - let "x" := Sample {@exp_bernoulli R [::] (1 / 4%:R)%:nng (p1S 3)} in + let "x" := Sample {@exp_bernoulli R [::] [{1 / 4}:R]} in let "r" := if #{"x"} then Score {5}:R else Score {2}:R in return #{"x"}]. -Let p511 : ((5%:R / 11%:R)%:nng%:num <= (1 : R)). -Proof. by rewrite /=; lra. Qed. - Lemma exec_bernoulli14_score : - execD bernoulli14_score = execD (exp_bernoulli (5%:R / 11%:R)%:nng p511). + execD bernoulli14_score = execD (exp_bernoulli [{5%:R / 11%:R}:R]). Proof. apply: eq_execD. rewrite execD_bernoulli/= execD_normalize_pt 2!execP_letin. @@ -421,39 +350,39 @@ under eq_integral. rewrite !letin'E. under eq_integral do rewrite retE /=. over. -rewrite !ge0_integral_measure_add //=; last by move=> b _; rewrite integral_ge0. -rewrite !ge0_integral_mscale //=; last 2 first. - by move=> b _; rewrite integral_ge0. - by move=> b _; rewrite integral_ge0. -rewrite !integral_dirac// !diracT !mul1e !iteE/=. -rewrite !ge0_integral_mscale//=. +rewrite /= integral_bernoulli//=; [|lra|by move=> b; rewrite integral_ge0]. +rewrite iteE/= !ge0_integral_mscale//=. rewrite ger0_norm//. rewrite !integral_cst//= !diracT !(mule1,mul1e). -rewrite !indicT/= !mulr1. +rewrite !indicT/= !mule1. +rewrite !iteE/= /mscale/=. rewrite ger0_norm//. +rewrite !diracT/= !mul1r. rewrite -EFinD/= eqe ifF; last first. apply/negbTE/negP => /orP[/eqP|//]. by rewrite /onem; lra. +rewrite integral_bernoulli//=; last lra. rewrite !letin'E/= !iteE/=. rewrite !ge0_integral_mscale//=. rewrite ger0_norm//. rewrite !integral_dirac//= !diracT !mul1e ger0_norm//. -rewrite /bernoulli/= measure_addE/= /mscale/= !mul1r. +rewrite bernoulliE//=; last lra. +rewrite !indicT. rewrite muleDl//; congr (_ + _)%E; rewrite -!EFinM; congr (_%:E); by rewrite !indicE /onem /=; case: (_ \in _); field. Qed. -End bernoulli_examples. +End sample_score. -Section binomial_examples. +Section sample_binomial. Context {R : realType}. Open Scope lang_scope. Open Scope ring_scope. Definition sample_binomial3 : @exp R _ [::] _ := - [let "x" := Sample {exp_binomial 3 (1 / 2)%:nng (p1S 1)} in + [let "x" := Sample {exp_binomial 3 [{1 / 2}:R]} in return #{"x"}]. Lemma exec_sample_binomial3 t U : measurable U -> @@ -463,46 +392,19 @@ Lemma exec_sample_binomial3 t U : measurable U -> (1 / 8)%:E * \d_3%N U)%E. Proof. move=> mU; rewrite /sample_binomial3 execP_letin execP_sample execP_return. -rewrite exp_var'E (execD_var_erefl "x") !execD_binomial/=. -rewrite letin'E ge0_integral_measure_sum//=. -rewrite !big_ord_recl big_ord0 !ge0_integral_mscale//=; [|exact: measurable_fun_dirac..]. -rewrite !integral_dirac// /bump; [|exact: measurable_fun_dirac..]. +rewrite exp_var'E (execD_var_erefl "x") !execD_binomial/= execD_real//=. +rewrite letin'E/= /= integral_binomial//=; [lra|move=> _]. +rewrite !big_ord_recl big_ord0/=. +rewrite /bump. rewrite !binS/= !bin0 bin1 bin2 bin_small// addn0. rewrite expr0 mulr1 mul1r subn0. rewrite -2!addeA !mul1r. congr _%:E. -rewrite indicT !mul1r /onem !addrA addr0 expr1/=. +rewrite !indicE /onem !addrA addr0 expr1/=. by congr (_ + _ + _ + _); congr (_ * _); field. Qed. -End binomial_examples. - -Section hard_constraint'. -Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). - -Definition fail' := - letin' (score (@measurable_cst _ _ X _ setT (0%R : R))) - (ret (@measurable_cst _ _ _ Y setT point)). - -Lemma fail'E x U : fail' x U = 0. -Proof. by rewrite /fail' letin'E ge0_integral_mscale//= normr0 mul0e. Qed. - -End hard_constraint'. -Arguments fail' {d d' X Y R}. - -(* hard constraints to express score below 1 *) -Lemma score_fail' d (X : measurableType d) {R : realType} - (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : - score (kr r%:num) = - letin' (sample_cst (bernoulli r1) : R.-pker X ~> _) - (ite macc0of2 (ret ktt) fail'). -Proof. -apply/eq_sfkernel => x U. -rewrite letin'E/= /sample; unlock. -rewrite ge0_integral_measure_add//= ge0_integral_mscale//= ge0_integral_mscale//=. -rewrite !integral_dirac//= !diracT/= !mul1e. -by rewrite /mscale/= iteE//= iteE//= fail'E mule0 adde0 ger0_norm. -Qed. +End sample_binomial. Section hard_constraint. Local Open Scope ring_scope. @@ -521,18 +423,20 @@ rewrite letin'E integral_indic//= /mscale/= normr0 mul0e. by rewrite /fail' letin'E/= ge0_integral_mscale//= normr0 mul0e. Qed. -Lemma exec_score_fail (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : - execP (g := [::]) [Score {r%:num}:R] = - execP [let str := Sample {exp_bernoulli r r1} in +Lemma exec_score_fail (r : R) (r01 : (0 <= r <= 1)%R) : + execP (g := [::]) [Score {r}:R] = + execP [let str := Sample {exp_bernoulli [{r}:R]} in if #str then return TT else {hard_constraint _}]. Proof. -rewrite execP_score execD_real /= score_fail'. +move: r01 => /andP[r0 r1]//. +rewrite execP_score execD_real /= score_fail' ?r0 ?r1//. rewrite execP_letin execP_sample/= execD_bernoulli execP_if execP_return. rewrite execD_unit/= exp_var'E /=. - apply/ctx_prf_head. + exact/ctx_prf_head (* TODO *). move=> h. apply: eq_sfkernel=> /= -[] U. -rewrite 2!letin'E/=. +rewrite [LHS]letin'E/= [RHS]letin'E/=. +rewrite execD_real/=. apply: eq_integral => b _. rewrite 2!iteE//=. case: b => //=. @@ -549,6 +453,206 @@ Qed. End hard_constraint. +Section test_uniform. +Local Open Scope ring_scope. +Local Open Scope lang_scope. +Context (R : realType). + +Definition uniform_syntax : @exp R _ [::] _ := + [let "p" := Sample {exp_uniform 0 1 (@ltr01 R)} in + return #{"p"}]. + +Lemma exec_uniform_syntax t U : measurable U -> + execP uniform_syntax t U = uniform_prob (@ltr01 R) U. +Proof. +move=> mU. +rewrite /uniform_syntax execP_letin execP_sample execP_return !execD_uniform. +rewrite exp_var'E (execD_var_erefl "p")/=. +rewrite letin'E /=. +rewrite integral_uniform//=; last exact: measurable_fun_dirac. +rewrite subr0 invr1 mul1e. +rewrite {1}/uniform_prob. +rewrite integral_mkcond//=. +rewrite [in RHS]integral_mkcond//=. +apply: eq_integral => x _. +rewrite !patchE. +case: ifPn => //; case: ifPn => //. +- move=> xU. + rewrite inE/= in_itv/= => x01. + by rewrite /uniform_pdf x01 diracE xU subr0 invr1. +- by rewrite diracE => /negbTE ->. +- move=> xU. + rewrite notin_setE/= in_itv/= => /negP/negbTE x01. + by rewrite /uniform_pdf x01. +Qed. + +End test_uniform. + +Section guard. +Local Open Scope ring_scope. +Local Open Scope lang_scope. +Context (R : realType). + +Definition guard : @exp R _ [::] _ := [ + let "p" := Sample {exp_bernoulli [{1 / 3}:R]} in + let "_" := if #{"p"} then return TT else Score {0}:R in + return #{"p"} +]. + +Lemma exec_guard t U : execP guard t U = ((1 / 3)%:E * \d_true U)%E. +Proof. +rewrite /guard 2!execP_letin execP_sample execD_bernoulli execD_real. +rewrite execP_if/= !execP_return !exp_var'E !(execD_var_erefl "p") execD_unit. +rewrite execP_score execD_real/=. +rewrite letin'E/= integral_bernoulli//=; last lra. +rewrite !letin'E !iteE/= integral_dirac// ge0_integral_mscale//=. +by rewrite normr0 mul0e !mule0 !adde0 !diracT !mul1e. +Qed. + +End guard. + +Section test_binomial. +Local Open Scope ring_scope. +Local Open Scope lang_scope. +Context (R : realType). + +Definition binomial_le : @exp R _ [::] Bool := + [let "a2" := Sample {exp_binomial 3 [{1 / 2}:R]} in + return {1}:N <= #{"a2"}]. + +Lemma exec_binomial_le t U : + execP binomial_le t U = ((7 / 8)%:E * \d_true U + + (1 / 8)%:E * \d_false U)%E. +Proof. +rewrite /binomial_le execP_letin execP_sample execP_return execD_rel execD_nat. +rewrite exp_var'E (execD_var_erefl "a2") execD_binomial/= !execD_real/=. +rewrite letin'E//= integral_binomial//=; [lra|move=> _]. +rewrite !big_ord_recl big_ord0//=. +rewrite /bump. +rewrite !binS/= !bin0 bin1 bin2 bin_small// addn0. +rewrite addeC adde0. +congr (_ + _)%:E. + rewrite !indicE !(mul0n,add0n,lt0n,mul1r)/=. + rewrite -!mulrDl; congr (_ * _). + rewrite /onem. + lra. +rewrite !expr0 ltnn indicE/= !(mul1r,mul1e) /onem. +lra. +Qed. + +Definition binomial_guard : @exp R _ [::] Nat := + [let "a1" := Sample {exp_binomial 3 [{1 / 2}:R]} in + let "_" := if #{"a1"} == {1}:N then return TT else Score {0}:R in + return #{"a1"}]. + +Lemma exec_binomial_guard t U : + execP binomial_guard t U = ((3 / 8)%:E * \d_1%N U)%E. +Proof. +rewrite /binomial_guard !execP_letin execP_sample execP_return execP_if. +rewrite !exp_var'E execD_rel !(execD_var_erefl "a1") execP_return. +rewrite execD_unit execD_binomial execD_nat execP_score !execD_real. +rewrite !letin'E//=. +rewrite integral_binomial//=; [lra|move=> _]. +rewrite !big_ord_recl big_ord0. +rewrite /bump/=. +rewrite !binS/= !bin0 bin1 bin2 bin_small//. +rewrite !letin'E//= !iteE/=. +rewrite !ge0_integral_mscale//=. +rewrite !integral_dirac//= !diracE/=. +rewrite /bump/=. +rewrite !(normr0,mul0e,mule0,add0e,add0n,mul1e,adde0). +rewrite mem_set//=. +rewrite /onem mul1e. +congr (_%:E * _)%E. +lra. +Qed. + +End test_binomial. + +Section beta_bernoulli_bernoulli. +Local Open Scope ring_scope. +Local Open Scope lang_scope. +Context (R : realType). +Local Notation mu := (@lebesgue_measure R). + +(* TODO: move? *) +Lemma integrable_bernoulli_XMonemX01 a b U + (mu : {measure set (g_sigma_algebraType R.-ocitv.-measurable) -> \bar R}) : + measurable U -> (mu `[0%R, 1%R]%classic < +oo)%E -> + mu.-integrable `[0, 1] (fun x => bernoulli (XMonemX01 a b x) U). +Proof. +move=> mU mu01oo. +apply/integrableP; split. + apply: (measurableT_comp (measurable_bernoulli2 _)) => //=. + by apply: measurable_funTS; exact: measurable_XMonemX01. +apply: (@le_lt_trans _ _ (\int[mu]_(x in `[0%R, 1%R]) cst 1 x)%E). + apply: ge0_le_integral => //=. + apply/measurable_funTS/measurableT_comp => //=. + apply: (measurableT_comp (measurable_bernoulli2 _)) => //=. + exact: measurable_XMonemX01. + by move=> x _; rewrite gee0_abs// probability_le1. +by rewrite integral_cst//= mul1e. +Qed. + +Let measurable_bernoulli_XMonemX01 U : + measurable_fun setT (fun x : R => bernoulli (XMonemX01 2 1 x) U). +Proof. +apply: (measurableT_comp (measurable_bernoulli2 _)) => //=. +exact: measurable_XMonemX01. +Qed. + +Lemma beta_bernoulli_bernoulli U : measurable U -> + @execP R [::] _ [let "p" := Sample {exp_beta 6 4} in + Sample {exp_bernoulli [#{"p"}]}] tt U = + @execP R [::] _ [Sample {exp_bernoulli [{3 / 5}:R]}] tt U. +Proof. +move=> mU. +rewrite execP_letin !execP_sample execD_beta !execD_bernoulli/=. +rewrite !execD_real/= exp_var'E (execD_var_erefl "p")/=. +transitivity (beta_prob_bernoulli 6 4 1 0 U : \bar R). + rewrite /beta_prob_bernoulli !letin'E/=. + rewrite integral_beta_prob//=; last 2 first. + exact: measurable_bernoulli2. + exact: integral_beta_prob_bernoulli_lty. + rewrite integral_beta_prob//=; last 2 first. + by apply: measurable_funTS => /=; exact: measurable_bernoulli_XMonemX01. + rewrite integral_beta_prob//=. + + suff: mu.-integrable `[0%R, 1%R] + (fun x => bernoulli (XMonemX01 2 1 x) U * (beta_pdf 6 4 x)%:E)%E. + move=> /integrableP[_]. + under eq_integral. + move=> x _. + rewrite gee0_abs//; last first. + by rewrite mule_ge0// lee_fin beta_pdf_ge0. + over. + move=> ?. + by under eq_integral do rewrite gee0_abs//. + + apply: integrableMl => //=. + * apply: integrable_bernoulli_XMonemX01 => //=. + by rewrite lebesgue_measure_itv//= lte01 EFinN sube0 ltry. + * by apply: measurable_funTS; exact: measurable_beta_pdf. + * exact: bounded_beta_pdf_01. + + apply/measurableT_comp => //; apply: measurable_funTS => /=. + exact: measurable_bernoulli_XMonemX01. + + under eq_integral do rewrite gee0_abs//=. + have : (beta_prob 6 4 `[0%R, 1%R] < +oo :> \bar R)%E. + by rewrite -ge0_fin_numE// beta_prob_fin_num. + by move=> /(@integrable_bernoulli_XMonemX01 2 1 _ (beta_prob 6 4) mU) /integrableP[]. + rewrite [RHS]integral_mkcond. + apply: eq_integral => x _ /=. + rewrite patchE. + case: ifPn => x01. + by rewrite /XMonemX01 patchE x01 XMonemX0' expr1. + by rewrite /beta_pdf /XMonemX01 patchE (negbTE x01) mul0r mule0. +rewrite beta_prob_bernoulliE// !bernoulliE//=; last 2 first. + lra. + by rewrite div_betafun_ge0 div_betafun_le1. +by congr (_ * _ + _ * _)%:E; + rewrite /div_betafun/= /onem !betafunE/= !factE/=; field. +Qed. + +End beta_bernoulli_bernoulli. + Section letinA. Local Open Scope lang_scope. Variable R : realType. @@ -597,20 +701,21 @@ Import Notations. Context {R : realType}. Definition staton_bus_syntax0 : @exp R _ [::] _ := - [let "x" := Sample {exp_bernoulli (2 / 7%:R)%:nng p27} in + [let "x" := Sample {exp_bernoulli [{2 / 7}:R]} in let "r" := if #{"x"} then return {3}:R else return {10}:R in let "_" := Score {exp_poisson 4 [#{"r"}]} in return #{"x"}]. Definition staton_bus_syntax := [Normalize {staton_bus_syntax0}]. -Let sample_bern : R.-sfker munit ~> mbool := sample_cst (bernoulli p27). +Let sample_bern : R.-sfker munit ~> mbool := + sample _ (measurableT_comp measurable_bernoulli (measurable_cst (2 / 7 : R)%R)). Let ite_3_10 : R.-sfker mbool * munit ~> measurableTypeR R := ite macc0of2 (@ret _ _ _ (measurableTypeR R) R _ (kr 3)) (@ret _ _ _ (measurableTypeR R) R _ (kr 10)). Let score_poisson4 : R.-sfker measurableTypeR R * (mbool * munit) ~> munit := - score (measurableT_comp (measurable_poisson 4) (@macc0of2 _ _ (measurableTypeR R) _)). + score (measurableT_comp (measurable_poisson_pdf 4) (@macc0of2 _ _ (measurableTypeR R) _)). Let kstaton_bus' := letin' sample_bern @@ -619,7 +724,8 @@ Let kstaton_bus' := Lemma eval_staton_bus0 : staton_bus_syntax0 -P> kstaton_bus'. Proof. -apply: eval_letin; first by apply: eval_sample; exact: eval_bernoulli. +apply: eval_letin. + by apply: eval_sample; apply: eval_bernoulli; exact: eval_real. apply: eval_letin. apply/evalP_if; [|exact/eval_return/eval_real..]. rewrite exp_var'E. @@ -634,7 +740,7 @@ Qed. Lemma exec_staton_bus0' : execP staton_bus_syntax0 = kstaton_bus'. Proof. -rewrite 3!execP_letin execP_sample/= execD_bernoulli. +rewrite 3!execP_letin execP_sample/= execD_bernoulli/= !execD_real. rewrite /kstaton_bus'; congr letin'. rewrite !execP_if !execP_return !execD_real/=. rewrite exp_var'E (execD_var_erefl "x")/=. @@ -651,7 +757,7 @@ Lemma exec_staton_bus : execD staton_bus_syntax = existT _ (normalize_pt kstaton_bus') (measurable_normalize_pt _). Proof. by rewrite execD_normalize_pt exec_staton_bus0'. Qed. -Let poisson4 := @poisson R 4%N. +Let poisson4 := @poisson_pdf R 4%N. Let staton_bus_probability U := ((2 / 7)%:E * (poisson4 3)%:E * \d_true U + @@ -661,19 +767,21 @@ Lemma exec_staton_bus0 (U : set bool) : execP staton_bus_syntax0 tt U = staton_bus_probability U. Proof. rewrite exec_staton_bus0' /staton_bus_probability /kstaton_bus'. -rewrite letin'_sample_bernoulli. +rewrite /sample_bern. +rewrite letin'E/=. +rewrite integral_bernoulli//=; last lra. rewrite -!muleA; congr (_ * _ + _ * _)%E. - rewrite letin'_iteT//. rewrite letin'_retk//. rewrite letin'_kret//. rewrite /score_poisson4. - by rewrite /score/= /mscale/= ger0_norm//= poisson_ge0. + by rewrite /score/= /mscale/= ger0_norm//= poisson_pdf_ge0. - by rewrite onem27. - rewrite letin'_iteF//. rewrite letin'_retk//. rewrite letin'_kret//. rewrite /score_poisson4. - by rewrite /score/= /mscale/= ger0_norm//= poisson_ge0. + by rewrite /score/= /mscale/= ger0_norm//= poisson_pdf_ge0. Qed. End staton_bus. @@ -686,7 +794,7 @@ Import Notations. Context {R : realType}. Definition staton_busA_syntax0 : @exp R _ [::] _ := - [let "x" := Sample {exp_bernoulli (2 / 7%:R)%:nng p27} in + [let "x" := Sample {exp_bernoulli [{2 / 7}:R]} in let "_" := let "r" := if #{"x"} then return {3}:R else return {10}:R in Score {exp_poisson 4 [#{"r"}]} in @@ -695,13 +803,14 @@ Definition staton_busA_syntax0 : @exp R _ [::] _ := Definition staton_busA_syntax : exp _ [::] _ := [Normalize {staton_busA_syntax0}]. -Let sample_bern : R.-sfker munit ~> mbool := sample_cst (bernoulli p27). +Let sample_bern : R.-sfker munit ~> mbool := + sample _ (measurableT_comp measurable_bernoulli (measurable_cst (2 / 7 : R)%R)). Let ite_3_10 : R.-sfker mbool * munit ~> measurableTypeR R := ite macc0of2 (@ret _ _ _ (measurableTypeR R) R _ (kr 3)) (@ret _ _ _ (measurableTypeR R) R _ (kr 10)). Let score_poisson4 : R.-sfker measurableTypeR R * (mbool * munit) ~> munit := - score (measurableT_comp (measurable_poisson 4) (@macc0of3' _ _ _ (measurableTypeR R) _ _)). + score (measurableT_comp (measurable_poisson_pdf 4) (@macc0of3' _ _ _ (measurableTypeR R) _ _)). (* same as kstaton_bus _ (measurable_poisson 4) but expressed with letin' instead of letin *) @@ -712,19 +821,10 @@ Let kstaton_busA' := score_poisson4) (ret macc1of3')). -(*Lemma kstaton_busA'E : kstaton_busA' = kstaton_bus _ (measurable_poisson 4). -Proof. -apply/eq_sfkernel => -[] U. -rewrite /kstaton_busA' /kstaton_bus. -rewrite letin'_letin. -rewrite /sample_bern. -congr (letin _ _ tt U). -rewrite 2!letin'_letin/=. -Abort.*) - Lemma eval_staton_busA0 : staton_busA_syntax0 -P> kstaton_busA'. Proof. -apply: eval_letin; first by apply: eval_sample; exact: eval_bernoulli. +apply: eval_letin. + by apply: eval_sample; apply: eval_bernoulli; exact: eval_real. apply: eval_letin. apply: eval_letin. apply/evalP_if; [|exact/eval_return/eval_real..]. @@ -739,7 +839,7 @@ Qed. Lemma exec_staton_busA0' : execP staton_busA_syntax0 = kstaton_busA'. Proof. -rewrite 3!execP_letin execP_sample/= execD_bernoulli. +rewrite 3!execP_letin execP_sample/= execD_bernoulli execD_real. rewrite /kstaton_busA'; congr letin'. rewrite !execP_if !execP_return !execD_real/=. rewrite exp_var'E (execD_var_erefl "x")/=. @@ -786,7 +886,7 @@ rewrite execP_return exp_var'E/= (execD_var_erefl "x") //=. by apply/eq_sfkernel => /= -[[] [a [b []]]] U0. Qed. -Let poisson4 := @poisson R 4%N. +Let poisson4 := @poisson_pdf R 4%N. Lemma exec_staton_busA0 U : execP staton_busA_syntax0 tt U = ((2 / 7%:R)%:E * (poisson4 3%:R)%:E * \d_true U + @@ -799,8 +899,6 @@ Section letinC. Local Open Scope lang_scope. Variable (R : realType). -Require Import Classical_Prop. (* TODO: mv *) - Let weak_head g {t1 t2} x (e : @exp R P g t2) (xg : x \notin dom g) := exp_weak P [::] _ (x, t1) e xg. diff --git a/theories/lang_syntax_examples_wip.v b/theories/lang_syntax_examples_wip.v deleted file mode 100644 index 6273470c0..000000000 --- a/theories/lang_syntax_examples_wip.v +++ /dev/null @@ -1,696 +0,0 @@ -Require Import String. -From HB Require Import structures. -From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. -From mathcomp Require Import mathcomp_extra boolp classical_sets. -From mathcomp Require Import functions cardinality fsbigop. -From mathcomp Require Import signed reals ereal topology normedtype sequences. -From mathcomp Require Import esum measure charge lebesgue_measure numfun. -From mathcomp Require Import lebesgue_integral kernel prob_lang. -From mathcomp Require Import lang_syntax_util lang_syntax lang_syntax_examples. -From mathcomp Require Import ring lra. - -(******************************************************************************) -(* Casino example *) -(* some steps *) - -Set Implicit Arguments. -Unset Strict Implicit. -Unset Printing Implicit Defensive. - -Import Order.TTheory GRing.Theory Num.Def Num.Theory. -Import numFieldTopology.Exports. - -Local Open Scope classical_set_scope. -Local Open Scope ring_scope. -(* Local Open Scope ereal_scope. *) - -Section beta_example. -Open Scope ring_scope. -Open Scope lang_scope. -Context {R : realType}. - -Lemma beta_bern610 : - @execP R [::] _ - [let "p" := - Sample {exp_beta 6 4} in Sample {exp_bernoulli_trunc [#{"p"}]}] = - execP [Sample {exp_bernoulli_trunc [{6 / 10}:R]}]. -Proof. -rewrite execP_letin !execP_sample !execD_beta_nat !execD_bernoulli_trunc/=. -rewrite execD_real exp_var'E !(execD_var_erefl "p")/=. -apply: eq_sfkernel=> x U. -rewrite letin'E/=. -transitivity ((\int[beta_nat 6 4]_(y in `[0%R, 1%R]) bernoulli_trunc y U)%E : \bar R). - rewrite [in RHS]integral_mkcond /=. - apply: eq_integral => y _. - rewrite patchE. - case: ifPn => //. - (* Unset Printing Notations. *) - simpl in *. - rewrite mem_setE /= in_itv /= negb_and -!ltNge => /orP[y0|y1]; - rewrite /bernoulli_trunc/=. - case: sumbool_ler. - move=> a. - by rewrite ltNge a in y0. - rewrite /prob_lang.bernoulli0 /bernoulli => _. - rewrite [LHS]measure_addE/= /mscale/=. - (* match default value *) - admit. - admit. - rewrite integral_beta_nat. -under eq_integral => y y01. - rewrite bernoulli_truncE. - rewrite muleC muleDr// !muleA muleC [X in _ + X]muleC. - over. - admit. -rewrite /= bernoulli_truncE. -rewrite integralD//. -congr (_ + _)%E. -rewrite integralZl//. -rewrite muleC. -congr (_ * _)%E. -(* rewrite /= setTI. *) -rewrite /beta_nat_pdf. -rewrite /ubeta_nat_pdf/ubeta_nat_pdf'/=. -transitivity (\int[@lebesgue_measure R]_(x0 in `[0%R, 1%R]) - ((ubeta_nat_pdf 7 4 x0 / beta_nat_norm 6 4)%:E))%E. - apply: eq_integral => p _. - rewrite muleC -EFinM. - rewrite -[X in X * _]divr1 mulf_div. - congr (_ / _)%:E; last by rewrite mul1r. - by rewrite /ubeta_nat_pdf/= /ubeta_nat_pdf' mulrA -exprS. -under eq_integral do rewrite mulrC EFinM. -rewrite integralZl//=. -rewrite -beta_nat_normE /beta_nat_norm/= factE. -rewrite -!EFinM/=. -congr _%:E; lra. - admit. - admit. -rewrite integralZl//. -rewrite muleC. -congr (_ * _)%E. -rewrite /beta_nat_pdf. -rewrite /ubeta_nat_pdf/ubeta_nat_pdf'/=. -transitivity (\int[@lebesgue_measure R]_(x0 in `[0%R, 1%R]) - ((ubeta_nat_pdf 6 5 x0 / beta_nat_norm 6 4)%:E))%E. - apply: eq_integral => p _. - by rewrite mulrC -EFinM -2!mulrA -exprSr mulrC. -under eq_integral do rewrite mulrC EFinM. -rewrite integralZl//=. -rewrite -beta_nat_normE /beta_nat_norm/= factE. -rewrite -!EFinM/onem/=. -congr _%:E; lra. - admit. - admit. - admit. - admit. - lra. -Admitted. - -End beta_example. - -Section casino_example. -Open Scope ring_scope. -Open Scope lang_scope. -Context (R : realType). -Lemma a01 : 0 < 1 - 0 :> R. Proof. by []. Qed. - -(* guard test *) -Definition test_guard : @exp R _ [::] _ := [ - let "p" := Sample {exp_bernoulli (1 / 3)%:nng (p1S 2)} in - let "_" := if #{"p"} then return TT else Score {0}:R in - return #{"p"} -]. - -Lemma exec_guard t U : execP test_guard t U = ((1 / 3)%:E * \d_true U)%E. -Proof. -rewrite /test_guard 2!execP_letin execP_sample execD_bernoulli execP_if/=. -rewrite !execP_return !exp_var'E !(execD_var_erefl "p") execD_unit execP_score execD_real/=. -rewrite letin'E ge0_integral_measure_sum//. -rewrite !big_ord_recl big_ord0 !ge0_integral_mscale//= !integral_dirac//. -rewrite !letin'E !iteE/= integral_dirac// ge0_integral_mscale//=. -by rewrite normr0 mul0e !mule0 !adde0 !diracT !mul1e. -Qed. - -Lemma binomial_over1 n p U : - 0 <= p <= 1 -> - (\int[binomial_probability_trunc n p]_y0 \d_(0 < y0)%N U = - bernoulli_trunc (1 - `1-p ^+ n) U :> \bar R)%E. -Proof. -move=> /andP[p0 p1]. -rewrite bernoulli_truncE; last first. - apply/andP; split. - apply/onemX_ge0; rewrite /onem; lra. - apply/onem_le1/exprn_ge0; rewrite /onem; lra. -rewrite (@integral_binomial_probabilty_trunc _ n p _ _ (fun y => \d_(1 <= y)%N U))//; last first. -rewrite !big_ord_recl/=. -rewrite /bump. -under eq_bigr => i _. - rewrite /=. - have -> : (0 < 1 + i)%N => //. - over. -rewrite addeC -ge0_sume_distrl. - congr (_ + _)%E; congr (_ * _)%E. - have -> : (\sum_(i < n) (p ^+ (1 + i) * `1-p ^+ (n - (1 + i)) *+ 'C(n, 1 + i))%:E)%E = - (\sum_(i < n.+1) (p ^+ i * `1-p ^+ (n - i) *+ 'C(n, i))%:E - (`1-p ^+ n)%:E)%E. - rewrite big_ord_recl/= expr0 subn0 mul1r bin0 mulr1n addeC addeA. - have <- : 0%E = ((- `1-p ^+ n)%:E + (`1-p ^+ n)%:E)%E. - rewrite EFinN. - congr _%:E. - lra. - by rewrite add0e. - congr _%E. - rewrite sumEFin. - rewrite !EFinB EFin_expe. - congr (_ - _)%E. - under eq_bigr do rewrite mulrC. - rewrite -(@exprDn_comm _ `1-p p n); last first. - by rewrite /GRing.comm/onem; lra. - rewrite /onem addrC. - have -> : p + (1 - p) = 1 by lra. - by rewrite expr1n. - rewrite subn0 expr0 bin0 mulr1n. - rewrite /onem. - congr _%:E. - set pn := (1-p) ^+ n. - lra. -move=> i _. -apply/mulrn_wge0/mulr_ge0; apply/exprn_ge0. -exact: p0. -apply/onem_ge0/p1. -Qed. - -Lemma binomial_le1 n p U : - 0 <= p <= 1 -> - (\int[binomial_probability_trunc n p]_y0 \d_(0 < y0)%N U = - \int[bernoulli_trunc (1 - `1-p ^+ n)]_y0 \d_y0 U :> \bar R)%E. -Proof. -move=> /andP[p0 p1]. -rewrite (@integral_bernoulli_trunc _ _ (fun x => \d_x U))//; last first. - apply/andP; split. - apply: onemX_ge0; rewrite /onem; lra. - apply/onem_le1/exprn_ge0; rewrite /onem; lra. -rewrite (@integral_binomial_probabilty_trunc _ n p _ _ (fun y => \d_(1 <= y)%N U))//; last first. -rewrite !big_ord_recl/=. -rewrite /bump. -under eq_bigr => i _. - rewrite /=. - have -> : (0 < 1 + i)%N => //. - over. -rewrite addeC -ge0_sume_distrl. - congr (_ + _)%E; congr (_ * _)%E. - have -> : (\sum_(i < n) (p ^+ (1 + i) * `1-p ^+ (n - (1 + i)) *+ 'C(n, 1 + i))%:E)%E = - (\sum_(i < n.+1) (p ^+ i * `1-p ^+ (n - i) *+ 'C(n, i))%:E - (`1-p ^+ n)%:E)%E. - rewrite big_ord_recl/= expr0 subn0 mul1r bin0 mulr1n addeC addeA. - have <- : 0%E = ((- `1-p ^+ n)%:E + (`1-p ^+ n)%:E)%E. - rewrite EFinN. - congr _%:E. - lra. - by rewrite add0e. - congr _%E. - rewrite sumEFin. - rewrite !EFinB EFin_expe. - congr (_ - _)%E. - under eq_bigr do rewrite mulrC. - rewrite -(@exprDn_comm _ `1-p p n); last first. - by rewrite /GRing.comm/onem; lra. - rewrite /onem addrC. - have -> : p + (1 - p) = 1 by lra. - by rewrite expr1n. - rewrite subn0 expr0 bin0 mulr1n. - rewrite /onem. - congr _%:E. - set pn := (1-p) ^+ n. - lra. -move=> i _. -apply/mulrn_wge0/mulr_ge0; apply/exprn_ge0. -exact: p0. -apply/onem_ge0/p1. -Qed. - -Lemma __ : uniform_probability a01 `[0, (1 / 2)] = (1 / 2)%:E. -Proof. -rewrite /uniform_probability /mscale/= /mrestr. -Abort. - -Lemma letin'_sample_uniform d d' (T : measurableType d) - (T' : measurableType d') (a b : R) (ab0 : (0 < b - a)%R) - (u : R.-sfker [the measurableType _ of (_ * T)%type] ~> T') x y : - measurable y -> - letin' (sample_cst (uniform_probability ab0)) u x y = - ((b - a)^-1%:E * \int[lebesgue_measure]_(x0 in `[a, b]) u (x0, x) y)%E. -Proof. -move=> my; rewrite letin'E/=. -rewrite integral_uniform//= => _ /= Y mY /=. -have /= := measurable_kernel u _ my measurableT _ mY. -move/measurable_ysection => /(_ x) /=. -set A := (X in measurable X). -set B := (X in _ -> measurable X). -suff : A = B by move=> ->. -rewrite {}/A {}/B !setTI /ysection/= (*TODO: lemma?*) /preimage/=. -by apply/seteqP; split => [z|z] /=; rewrite inE/=. -Qed. - -Lemma execP_letin_uniform g t str (s0 s1 : exp P ((str, Real) :: g) t) : - (forall (p : R) x U, 0 <= p <= 1 -> - execP s0 (p, x) U = execP s1 (p, x) U) -> - forall x U, measurable U -> - execP [let str := Sample {@exp_uniform _ g 0 1 a01} in {s0}] x U = - execP [let str := Sample {@exp_uniform _ g 0 1 a01} in {s1}] x U. -Proof. -move=> s01 x U mU. -rewrite !execP_letin execP_sample execD_uniform/=. -rewrite !letin'_sample_uniform//. -congr (_ * _)%E. -apply: eq_integral => p p01. -apply: s01. -by rewrite inE in p01. -Qed. - -(* Lemma measurable_mtyp (t : typ) (U : set (@mtyp R t)) : measurable U. -Proof. -induction t => //. *) - -Lemma congr_letinl g t1 t2 str (e1 e2 : @exp _ _ g t1) -(e : @exp _ _ (_ :: g) t2) x U : - (forall y V, execP e1 y V = execP e2 y V) -> - measurable U -> - @execP R g t2 [let str := e1 in e] x U = - @execP R g t2 [let str := e2 in e] x U. -Proof. -move=> He mU. -apply eq_sfkernel in He. -by rewrite !execP_letin He. -Qed. - -Lemma congr_letin g t1 t2 str (e : @exp _ _ _ t1) (e1 e2 : @exp _ _ (_ :: g) t2) x U : - (forall y V, execP e1 (y, x) V = execP e2 (y, x) V) -> - @execP R g t2 [let str := e in e1] x U = @execP R g t2 [let str := e in e2] x U. -Proof. -move=> He. -rewrite !execP_letin !letin'E. -apply: eq_integral => ? _. -apply: He. -Qed. - -Lemma congr_normalize g t (e1 e2 : @exp R _ g t) : - (forall x U, execP e1 x U = execP e2 x U) -> - execD [Normalize e1] = execD [Normalize e2]. -Proof. -move=> He. -apply: eq_execD. -rewrite !execD_normalize_pt /=. -f_equal. -apply: eq_kernel => y V. -apply: He. -Qed. - -Definition uniform_syntax : @exp R _ [::] _ := - [let "p" := Sample {exp_uniform 0 1 a01} in - return #{"p"}]. - -Lemma exec_uniform_syntax t U : measurable U -> - execP uniform_syntax t U = uniform_probability a01 U. -Proof. -move=> mU. -rewrite /uniform_syntax execP_letin execP_sample execP_return !execD_uniform. -rewrite exp_var'E (execD_var_erefl "p")/=. -rewrite letin'E /=. -rewrite integral_uniform//=; last exact: measurable_fun_dirac. -rewrite subr0 invr1 mul1e. -rewrite {1}/uniform_probability. -rewrite /mscale/= subr0 invr1 mul1e. -by rewrite integral_indic. -Qed. - -Definition binomial_le : @exp R _ [::] Bool := - [let "a2" := Sample {exp_binomial 3 (1 / 2)%:nng (p1S 1)} in - return {1}:N <= #{"a2"}]. - -Lemma exec_binomial_le t U : - execP binomial_le t U = ((7 / 8)%:E * \d_true U + - (1 / 8)%:E * \d_false U)%E. -Proof. -rewrite /binomial_le execP_letin execP_sample execP_return execD_rel execD_nat. -rewrite exp_var'E (execD_var_erefl "a2") execD_binomial. -rewrite letin'E//= /binomial_probability ge0_integral_measure_sum//=. -rewrite !big_ord_recl big_ord0 !ge0_integral_mscale//=. -rewrite !integral_dirac// /bump. -rewrite !binS/= !bin0 bin1 bin2 bin_small// addn0. -rewrite addeC adde0. -congr (_ + _)%:E. - rewrite !indicT !(mul0n,add0n,lt0n,mul1r)/=. - rewrite -!mulrDl; congr (_ * _). - rewrite /onem. - lra. -rewrite !expr0 ltnn indicT/= !(mul1r,mul1e) /onem. -lra. -Qed. - -Definition binomial_guard : @exp R _ [::] Nat := - [let "a1" := Sample {exp_binomial 3 (1 / 2)%:nng (p1S 1)} in - let "_" := if #{"a1"} == {1}:N then return TT else Score {0}:R in - return #{"a1"}]. - -Lemma exec_binomial_guard t U : - execP binomial_guard t U = ((3 / 8)%:E * \d_1%N U(* + - (1 / 8)%:E * \d_0%N U*))%E. -Proof. -rewrite /binomial_guard !execP_letin execP_sample execP_return execP_if. -rewrite !exp_var'E execD_rel !(execD_var_erefl "a1") execP_return. -rewrite execD_unit execD_binomial execD_nat execP_score execD_real. -rewrite !letin'E//= /binomial_probability ge0_integral_measure_sum//=. -rewrite !big_ord_recl big_ord0 !ge0_integral_mscale//=. -rewrite !integral_dirac//. -rewrite /bump/=. -rewrite !binS/= !bin0 bin1 bin2 bin_small//. -rewrite !diracT !addn0 !expr0 !subn0 !mulr1n !mul1r !expr1 !mul1e. -rewrite !letin'E//= !iteE/= !diracE/=. -rewrite !ge0_integral_mscale//=. -rewrite !integral_dirac// !diracT//. -rewrite !(normr0,mul0e,mule0,add0e,add0n,mul1e,adde0). -rewrite /onem. -congr (_%:E * _)%E. -lra. -Qed. - -Lemma exec_beta_a1 U : - @execP R [::] _ [let "p" := Sample {exp_beta 6 4} in - Sample {exp_bernoulli_trunc [#{"p"}]}] tt U = - @execP R [::] _ [Sample {exp_bernoulli_trunc [{3 / 5}:R]}] tt U. -Proof. -rewrite execP_letin !execP_sample execD_beta_nat !execD_bernoulli_trunc/=. -rewrite !execD_real/= exp_var'E (execD_var_erefl "p")/=. -transitivity (beta_nat_bern 6 4 1 0 U : \bar R). - rewrite /beta_nat_bern !letin'E/= /ubeta_nat_pdf/= /onem. - apply: eq_integral => x _. - rewrite /=. - do 2 f_equal. - by rewrite /ubeta_nat_pdf' expr1 expr0 mulr1. -rewrite beta_nat_bern_bern// !bernoulli_truncE; last 2 first. - by lra. - apply/andP; split. - by apply/Baa'bb'Bab_ge0. - by apply/Baa'bb'Bab_le1. -congr (_ * _ + _ * _)%:E. - rewrite /Baa'bb'Bab /beta_nat_norm/=. - rewrite !factS/= fact0. - by field. -rewrite /onem; rewrite /Baa'bb'Bab /beta_nat_norm/=; -rewrite !factS/= fact0. -by field. -Qed. - -Definition casino0 : @exp R _ [::] _ := - [Normalize - let "p" := Sample {exp_uniform 0 1 a01} in - let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in - let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in - let "a2" := Sample {exp_binomial_trunc 3 [#{"p"}]} in - return {1}:N <= #{"a2"}]. - -Definition casino1 : @exp R _ [::] _ := - [Normalize - let "p" := Sample {exp_uniform 0 1 a01} in - let "a1" := Sample {exp_binomial_trunc 8 [#{"p"}]} in - let "_" := if #{"a1"} == {5}:N then return TT else Score {0}:R in - Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. - -Definition casino2 : @exp R _ [::] _ := - [Normalize - let "p" := Sample {exp_uniform 0 1 a01} in - let "_" := - Score {[{56}:R * #{"p"} ^+ {5%nat} * {[{1}:R - #{"p"}]} ^+ {3%nat}]} in - Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. - -Definition casino2' : @exp R _ [::] _ := - [Normalize - let "p" := Sample {exp_beta 1 1} in - let "_" := Score - {[{56}:R * #{"p"} ^+ {5%nat} * {[{1}:R - #{"p"}]} ^+ {3%nat}]} in - Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. - -Definition casino3 : @exp R _ [::] _ := - [Normalize - let "_" := Score {1 / 9}:R in - let "p" := Sample {exp_beta 6 4} in - Sample {exp_bernoulli_trunc [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%nat}]}]. - -Definition casino4 : @exp R _ [::] _ := - [Normalize - let "_" := Score {1 / 9}:R in - Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. - -Definition casino5 : @exp R _ [::] _ := - [Normalize Sample {exp_bernoulli_trunc [{10 / 11}:R]}]. - -Lemma casino01 : - execD casino0 = execD casino1. -Proof. -rewrite /casino0 /casino1. -apply: eq_execD. -f_equal. -apply: congr_normalize => y V. -apply: execP_letin_uniform => //. -move=> p x U r01. -apply: congr_letin => y0 V0. -apply: congr_letin => y1 V1. -rewrite !execP_letin !execP_sample !execD_binomial_trunc /=. -rewrite !execP_return !execD_bernoulli_trunc/=. -rewrite !execD_rel (@execD_bin _ _ binop_minus) execD_pow. -rewrite (@execD_bin _ _ binop_minus) !execD_real/= !execD_nat. -rewrite !exp_var'E !(execD_var_erefl "p") !(execD_var_erefl "a2")/=. -rewrite !letin'E/=. -move: r01 => /andP[r0 r1]. -by apply/binomial_over1/andP. -Qed. - -Lemma casino12 : - execD casino1 = execD casino2. -Proof. -apply: eq_execD. -f_equal. -apply: congr_normalize => y V. -apply: execP_letin_uniform => //. -move=> p x U /andP[p0 p1]. -rewrite !execP_letin !execP_sample execP_if execD_rel/=. -rewrite !execP_score !(@execD_bin _ _ binop_mult). -rewrite !execD_bernoulli_trunc/= !(@execD_bin _ _ binop_minus) !execD_pow. -rewrite !(@execD_bin _ _ binop_minus)/=. -rewrite !execD_real !execD_nat/= execP_return execD_unit. -rewrite !execD_binomial_trunc/=. -rewrite !exp_var'E !(execD_var_erefl "p") !(execD_var_erefl "a1")/=. -rewrite !letin'E/=. -rewrite integral_binomial_probabilty_trunc//=. -rewrite (bigD1 (inord 5))//=. - rewrite big1; last first. - move=> [[|[|[|[|[|[|[|[|[|//]]]]]]]]]]//= Hi Hi5; rewrite letin'E iteE; - rewrite ?ge0_integral_mscale//= ?normr0 ?mul0e ?mule0 ?add0e//. - suff: false by []. - move/negbTE: Hi5 => <-. - by apply/eqP/val_inj => /=; rewrite inordK. -rewrite letin'E iteE ge0_integral_mscale//= inordK//= adde0 /onem. -congr (_ * _)%E. -rewrite ger0_norm. - by rewrite -mulrA mulr_natl. -apply/mulr_ge0. - exact/mulr_ge0/exprn_ge0. -apply/exprn_ge0. -by rewrite subr_ge0. -Qed. - -Lemma casino22' : - execD casino2 = execD casino2'. -Proof. -apply: eq_execD. -f_equal. -apply: congr_normalize => //= x U. -apply: congr_letinl => //= y V. -rewrite !execP_sample execD_uniform execD_beta_nat/=. -rewrite beta11_uniform//. -rewrite /=. -apply: sub_sigma_algebra. -rewrite //. -admit. -Admitted. - -Lemma casino23 : - execD casino2' = execD casino3. -Proof. -apply: eq_execD. -f_equal. -apply: congr_normalize => x U. -rewrite !execP_letin !execP_sample !execP_score !execD_beta_nat. -rewrite !execD_bernoulli_trunc/= !(@execD_bin _ _ binop_mult). -rewrite !execD_pow !(@execD_bin _ _ binop_minus) !execD_real/=. -rewrite !execD_pow !(@execD_bin _ _ binop_minus) !execD_real/=. -rewrite !exp_var'E !(execD_var_erefl "p")/=. -rewrite !letin'E/= ![in RHS]ge0_integral_mscale//=. -rewrite /=. -under eq_integral => y _. - rewrite letin'E/= /mscale/=. - over. -rewrite /=. -(* set f := letin' _ _. *) -(* oops... -transitivity (\int[beta_nat 1 1]_(y in `[0%R, 1%R]) f (y, x) U)%E. - rewrite [in RHS]integral_mkcond /=. - apply: eq_integral => y _. - rewrite patchE. - case: ifPn => //. - simpl in *. - rewrite mem_setE /= in_itv /= negb_and -!ltNge => /orP[y0|y1]; - rewrite /bernoulli_trunc/=. - case: sumbool_ler. - move=> a. - by rewrite ltNge a in y0. - rewrite /prob_lang.bernoulli0 /bernoulli => _. - rewrite [LHS]measure_addE/= /mscale/=. - (* match default value *) - admit. - admit. - rewrite integral_beta_nat. - admit. -rewrite (integral_beta_nat 6 4). - rewrite ger0_norm// integral_dirac// diracT mul1e letin'E/=. - transitivity (((1 / 9)%:E * \int[beta_nat 6 4]_(y in `[0%R, 1%R]) - bernoulli_trunc (1 - (1 - y) ^+ 3) U)%E : \bar R); last first. - admit. -rewrite (integral_beta_nat 6 4)//=. -rewrite -integralZl//=. - apply: eq_integral => y y01. - rewrite /f letin'E /= !ge0_integral_mscale//= integral_dirac// diracT mul1e. - rewrite -muleAC muleC -[in RHS]muleCA. - congr (_ * _)%E. - rewrite ger0_norm. - rewrite /beta_nat_pdf ubeta_nat_pdf11/= muleC -!EFinM. - rewrite !div1r. - rewrite /beta_nat_norm/= /ubeta_nat_pdf/ubeta_nat_pdf' factE/=/onem. - congr _%:E; lra. - rewrite inE/= in_itv/= in y01. - move: y01 => /andP[y0 y1]. - apply/mulr_ge0/exprn_ge0 => //. - apply/mulr_ge0/exprn_ge0 => //. - lra. -admit. -admit. -admit. -apply: (@measurableT_comp _ _ _ _ _ _ (fun x0 => f x0 U)). - apply: measurable_kernel. - done. -apply: measurable_pair2. -admit.*) -Admitted. - -Lemma casino34' U : - @execP R [::] _ [let "p" := Sample {exp_beta 6 4} in - Sample {exp_bernoulli_trunc [{[{1}:R - #{"p"}]} ^+ {3%nat}]}] tt U = - @execP R [::] _ [Sample {exp_bernoulli_trunc [{1 / 11}:R]}] tt U. -Proof. -rewrite execP_letin !execP_sample execD_beta_nat !execD_bernoulli_trunc/=. -rewrite execD_pow/= (@execD_bin _ _ binop_minus) !execD_real/=. -rewrite exp_var'E (execD_var_erefl "p")/=. -(* TODO: generalize *) -rewrite letin'E/=. -have := (@beta_nat_bern_bern R 6 4 0 3 U). -rewrite /beta_nat_bern /ubeta_nat_pdf/ubeta_nat_pdf'/=. -under eq_integral do rewrite expr0 mul1r. -move=> ->//. -rewrite /Baa'bb'Bab addn0 /beta_nat_norm/= factE/=. -by congr (bernoulli_trunc _ _); field. -Qed. - -Lemma bern_onem (f : _ -> R) U p : - (forall x, 0 <= f x <= 1) -> - (\int[beta_nat 6 4]_y bernoulli_trunc (f y) U = - p%:E * \d_true U + `1-p%:E * \d_false U)%E -> - (\int[beta_nat 6 4]_y bernoulli_trunc (1 - f y) U - = `1-p%:E * \d_true U + p%:E * \d_false U)%E. -Proof. -move=> f01. -under eq_integral => x _. - rewrite bernoulli_truncE. - over. -done. -move=> h1. -rewrite /= in h1. -rewrite /bernoulli_trunc. -(* /beta_nat/mscale/= /beta_nat_norm/= /ubeta_nat/ubeta_nat_pdf/=. *) -Admitted. - -Lemma casino34 : - execD casino3 = execD casino4. -Proof. -apply: eq_execD. -f_equal. -apply: congr_normalize => y V. -apply: congr_letin => x U. -rewrite execP_letin !execP_sample execD_beta_nat !execD_bernoulli_trunc/=. -rewrite (@execD_bin _ _ binop_minus) execD_pow/= (@execD_bin _ _ binop_minus). -rewrite !execD_real/= exp_var'E (execD_var_erefl "p")/=. -transitivity (\int[beta_nat 6 4]_y bernoulli_trunc (1 - (1 - y) ^+ 3) U : \bar R)%E. - by rewrite /beta_nat_bern !letin'E/= /onem. -rewrite bernoulli_truncE; last by lra. -have -> := (@bern_onem (fun x => (1 - x) ^+ 3) U (1 / 11) _). - congr (_ * _ + _ * _)%E; congr _%:E; rewrite /onem; lra. - admit. -transitivity (beta_nat_bern 6 4 0 3 U : \bar R). - rewrite /beta_nat_bern /ubeta_nat_pdf/= /onem. - apply: eq_integral => y0 _. - do 2 f_equal. - rewrite /ubeta_nat_pdf'/=. - rewrite expr0. - by rewrite mul1r. -rewrite beta_nat_bern_bern//= bernoulli_truncE; last first. - apply/andP; split. - apply/Baa'bb'Bab_ge0. - apply/Baa'bb'Bab_le1. -congr (_ * _ + _ * _)%:E; rewrite /onem. - rewrite /Baa'bb'Bab /beta_nat_norm/=. - by rewrite !factS/= fact0; field. -rewrite /Baa'bb'Bab /beta_nat_norm/=. -by rewrite !factS/= fact0; field. -Admitted. - -Lemma norm_score_bern g p1 p2 (p10 : p1 != 0) (p1_ge0 : 0 <= p1) -(p201 : 0 <= p2 <= 1) : - @execD R g _ - [Normalize let "_" := Score {p1}:R in - Sample {exp_bernoulli_trunc [{p2}:R]}] = - execD [Normalize Sample {exp_bernoulli_trunc [{p2}:R]}]. -Proof. -apply: eq_execD. -rewrite !execD_normalize_pt/= !execP_letin !execP_score. -rewrite !execP_sample !execD_bernoulli_trunc !execD_real/=. -apply: funext=> x. -apply: eq_probability=> /= y. -rewrite /normalize_pt !normalizeE/=. -rewrite !bernoulli_truncE; last lra; last lra. -rewrite !diracT !mule1 /onem -EFinD addrCA subrr addr0. -rewrite !letin'E. -under eq_integral. - move=> x0 _ /=. - rewrite !bernoulli_truncE; last lra. - rewrite !diracT !mule1 /onem -EFinD addrCA subrr addr0. - over. -rewrite !ge0_integral_mscale//= ger0_norm//. -rewrite integral_dirac// diracT !mule1. -rewrite !ifF; last first. - rewrite eqe. - apply/negbTE/negP => /orP[/eqP|//]. - by rewrite /onem; lra. - rewrite eqe. - apply/negbTE/negP => /orP[/eqP|//]. - by rewrite /onem; lra. -rewrite !bernoulli_truncE; last lra. -rewrite integral_dirac//= diracT !diracE. -by rewrite muleC muleA -EFinM mulVf// invr1 /onem !(mul1r, mule1). -Qed. - -Lemma casino45 : - execD casino4 = execD casino5. -Proof. -rewrite norm_score_bern//. -lra. -Qed. - -End casino_example. diff --git a/theories/lang_syntax_table_game.v b/theories/lang_syntax_table_game.v new file mode 100644 index 000000000..f25dac710 --- /dev/null +++ b/theories/lang_syntax_table_game.v @@ -0,0 +1,717 @@ +Require Import String. +From HB Require Import structures. +From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. +From mathcomp Require Import mathcomp_extra boolp classical_sets. +From mathcomp Require Import functions cardinality fsbigop. +From mathcomp Require Import signed reals ereal topology normedtype sequences. +From mathcomp Require Import esum measure charge lebesgue_measure numfun. +From mathcomp Require Import lebesgue_integral probability kernel prob_lang. +From mathcomp Require Import lang_syntax_util lang_syntax lang_syntax_examples. +From mathcomp Require Import ring lra. + +(**md**************************************************************************) +(* # Eddy's table game example *) +(* *) +(* ref: *) +(* - Chung-chieh Shan, Equational reasoning for probabilistic programming, *) +(* POPL TutorialFest 2018 *) +(* https://homes.luddy.indiana.edu/ccshan/rational/equational-handout.pdf *) +(* - Sean R Eddy, What is Bayesian statistics?, Nature Biotechnology 22(9), *) +(* 1177--1178 (2004) *) +(* *) +(******************************************************************************) + +Set Implicit Arguments. +Unset Strict Implicit. +Unset Printing Implicit Defensive. + +Import Order.TTheory GRing.Theory Num.Def Num.Theory. +Import numFieldTopology.Exports. + +Local Open Scope classical_set_scope. +Local Open Scope ring_scope. + +Local Open Scope ereal_scope. +Lemma letin'_sample_uniform {R : realType} d d' (T : measurableType d) + (T' : measurableType d') (a b : R) (ab : (a < b)%R) + (u : R.-sfker [the measurableType _ of (_ * T)%type] ~> T') x y : + measurable y -> + letin' (sample_cst (uniform_prob ab)) u x y = + (b - a)^-1%:E * \int[lebesgue_measure]_(x0 in `[a, b]) u (x0, x) y. +Proof. +move=> my; rewrite letin'E/=. +rewrite integral_uniform//=. +move => _ /= Y mY /=. +have /= := measurable_kernel u _ my measurableT _ mY. +move/measurable_ysection => /(_ x) /=. +set A := (X in measurable X). +set B := (X in _ -> measurable X). +suff : A = B by move=> ->. +by rewrite {}/A {}/B !setTI ysectionE. +Qed. + +Local Open Scope lang_scope. +Lemma execP_letin_uniform {R : realType} + g t str (s0 s1 : exp P ((str, Real) :: g) t) : + (forall (p : R) x U, (0 <= p <= 1)%R -> + execP s0 (p, x) U = execP s1 (p, x) U) -> + forall x U, measurable U -> + execP [let str := Sample {@exp_uniform _ g 0 1 (@ltr01 R)} in {s0}] x U = + execP [let str := Sample {@exp_uniform _ g 0 1 (@ltr01 R)} in {s1}] x U. +Proof. +move=> s01 x U mU. +rewrite !execP_letin execP_sample execD_uniform/=. +rewrite !letin'_sample_uniform//. +congr *%E. +apply: eq_integral => p p01. +apply: s01. +by rewrite inE in p01. +Qed. +Local Close Scope lang_scope. +Local Close Scope ereal_scope. + +Section bounded. +Local Open Scope ring_scope. +Local Open Scope lang_scope. +Local Open Scope ereal_scope. +Context {R : realType}. + +Lemma bounded_id_01 : [bounded x0 : R^o | x0 in `[0%R, 1%R]%classic : set R]. +Proof. +exists 1%R; split => // y y1. +near=> M => /=. +rewrite (le_trans _ (ltW y1))//. +near: M. +move=> M /=. +rewrite in_itv/= => /andP[M0 M1]. +by rewrite ler_norml M1 andbT (le_trans _ M0). +Unshelve. all: by end_near. Qed. + +Lemma bounded_onem_01 : [bounded (`1- x : R^o) | x in `[0%R, 1%R]%classic : set R]. +Proof. +exists 1%R; split => // y y1. +near=> M => /=. +rewrite (le_trans _ (ltW y1))//. +near: M. +move=> M /=. +rewrite in_itv/= => /andP[M0 M1]. +rewrite ler_norml (@le_trans _ _ 0%R)//=. + by rewrite lerBlDr addrC -lerBlDr subrr. +by rewrite onem_ge0. +Unshelve. all: by end_near. Qed. + +Lemma bounded_cst_01 (x : R) : [bounded x : R^o | _ in `[0%R, 1%R]%classic : set R]. +Proof. +exists `|x|%R; split. + by rewrite num_real. +move=> y y1/= z. +rewrite in_itv/= => /andP[z0 z1]. +by rewrite (le_trans _ (ltW y1)). +Qed. + +Lemma bounded_norm (f : R -> R) : + [bounded f x : R^o | x in (`[0%R, 1%R]%classic : set R)] <-> + [bounded `|f x|%R : R^o | x in (`[0%R, 1%R]%classic : set R)]. +Proof. +split. + move=> [M [Mreal HM]]. + exists `|M|%R; split; first by rewrite normr_real. + move=> r Mr x/= x01. + by rewrite ger0_norm// HM// (le_lt_trans _ Mr)// ler_norm. +move=> [M [Mreal HM]]. +exists `|M|%R; split; first by rewrite normr_real. +move=> r Mr x/= x01. +rewrite -[leLHS]ger0_norm// HM//. +by rewrite (le_lt_trans _ Mr)// ler_norm. +Qed. + +Lemma boundedMl k (f : R -> R) : + [bounded f x : R^o | x in (`[0%R, 1%R]%classic : set R)] -> + [bounded (k * f x)%R : R^o | x in (`[0%R, 1%R]%classic : set R)]. +Proof. +move=> [M [Mreal HM]]. +exists `|k * M|%R; split; first by rewrite normr_real. +move=> r kMr x/= x01. +rewrite normrM. +have [->|k0] := eqVneq k 0%R. + by rewrite normr0 mul0r (le_trans _ (ltW kMr)). +rewrite -ler_pdivlMl ?normr_gt0//. +apply: HM => //. +rewrite ltr_pdivlMl ?normr_gt0//. +rewrite (le_lt_trans _ kMr)//. +by rewrite normrM ler_pM2l ?normr_gt0// ler_norm. +Qed. + +End bounded. + +Lemma measurable_bernoulli_expn {R : realType} U n : + measurable_fun [set: g_sigma_algebraType R.-ocitv.-measurable] + (fun x : g_sigma_algebraType (R.-ocitv.-measurable) => bernoulli (`1-x ^+ n) U). +Proof. +apply: (measurableT_comp (measurable_bernoulli2 _)) => //=. +by apply: measurable_funX => //=; exact: measurable_funB. +Qed. + +Lemma integrable_bernoulli_beta_pdf {R : realType} U : measurable U -> + (@lebesgue_measure R).-integrable [set: g_sigma_algebraType R.-ocitv.-measurable] + (fun x => bernoulli (1 - `1-x ^+ 3) U * (beta_pdf 6 4 x)%:E)%E. +Proof. +move=> mU. +have ? : measurable_fun [set: g_sigma_algebraType R.-ocitv.-measurable] + (fun x => bernoulli (1 - `1-x ^+ 3) U). + apply: (measurableT_comp (measurable_bernoulli2 _)) => //=. + apply: measurable_funB => //; apply: measurable_funX => //. + exact: measurable_funB. +apply/integrableP; split => /=. + apply: emeasurable_funM => //=. + by apply/measurable_EFinP; exact: measurable_beta_pdf. +apply: (@le_lt_trans _ _ (\int[lebesgue_measure]_(x in `[0%R, 1%R]) (beta_pdf 6 4 x)%:E))%E. + rewrite [leRHS]integral_mkcond /=. + apply: ge0_le_integral => //=. + - apply: measurableT_comp => //; apply: emeasurable_funM => //. + by apply/measurable_EFinP; exact: measurable_beta_pdf. + - move=> x _ /=; rewrite patchE; case: ifPn => // _. + by rewrite lee_fin beta_pdf_ge0. + - apply/(measurable_restrict _ _ _) => //. + apply/measurable_funTS/measurableT_comp => //. + exact: measurable_beta_pdf. + - move=> x _. + rewrite patchE; case: ifPn => x01. + rewrite gee0_abs//. + rewrite gee_pMl// ?probability_le1//. + by rewrite ge0_fin_numE// (le_lt_trans (probability_le1 _ _))// ltry. + by rewrite lee_fin beta_pdf_ge0. + by rewrite mule_ge0// lee_fin beta_pdf_ge0. + by rewrite /beta_pdf /XMonemX01 patchE (negbTE x01) mul0r mule0 abse0. +apply: (@le_lt_trans _ _ + (\int[lebesgue_measure]_(x in `[0%R, 1%R]) (betafun 6 4)^-1%:E)%E); last first. + by rewrite integral_cst//= lebesgue_measure_itv/= lte01 EFinN sube0 mule1 ltry. +apply: ge0_le_integral => //=. +- by move=> ? _; rewrite lee_fin beta_pdf_ge0. +- by apply/measurable_funTS/measurableT_comp => //; exact: measurable_beta_pdf. +- by move=> ? _; rewrite lee_fin invr_ge0// betafun_ge0. +- by move=> x _; rewrite lee_fin beta_pdf_le_betafunV. +Qed. + +Section game_programs. +Local Open Scope ring_scope. +Local Open Scope ereal_scope. +Local Open Scope lang_scope. +Context (R : realType). +Local Notation mu := lebesgue_measure. + +Definition guard {g} str n : @exp R P [:: (str, _) ; g] _ := + [if #{str} == {n}:N then return TT else Score {0}:R]. + +Definition prog0 : @exp R _ [::] _ := + [Normalize + let "p" := Sample {exp_uniform 0 1 (@ltr01 R)} in + let "x" := Sample {exp_binomial 8 [#{"p"}]} in + let "_" := {guard "x" 5} in + let "y" := Sample {exp_binomial 3 [#{"p"}]} in + return {1}:N <= #{"y"}]. + +Definition tail1 : @exp R _ [:: ("_", Unit); ("x", Nat) ; ("p", Real)] _ := + [Sample {exp_bernoulli [{1}:R - {[{1}:R - #{"p"}]} ^+ {3}]}]. + +Definition tail2 : @exp R _ [:: ("_", Unit); ("p", Real)] _ := + [Sample {exp_bernoulli [{1}:R - {[{1}:R - #{"p"}]} ^+ {3}]}]. + +Definition tail3 : @exp R _ [:: ("p", Real); ("_", Unit)] _ := + [Sample {exp_bernoulli [{1}:R - {[{1}:R - #{"p"}]} ^+ {3}]}]. + +Definition prog1 : @exp R _ [::] _ := + [Normalize + let "p" := Sample {exp_uniform 0 1 (@ltr01 R)} in + let "x" := Sample {exp_binomial 8 [#{"p"}]} in + let "_" := {guard "x" 5} in + {tail1}]. + +Definition prog2 : @exp R _ [::] _ := + [Normalize + let "p" := Sample {exp_uniform 0 1 (@ltr01 R)} in + let "_" := + Score {[{56}:R * #{"p"} ^+ {5} * {[{1}:R - #{"p"}]} ^+ {3}]} in + {tail2}]. + +Definition prog2' : @exp R _ [::] _ := + [Normalize + let "p" := Sample {exp_beta 1 1} in + let "_" := Score + {[{56}:R * #{"p"} ^+ {5} * {[{1}:R - #{"p"}]} ^+ {3}]} in + {tail2}]. + +Definition prog3 : @exp R _ [::] _ := + [Normalize + let "_" := Score {1 / 9}:R in + let "p" := Sample {exp_beta 6 4} in + {tail3}]. + +Definition prog4 : @exp R _ [::] _ := + [Normalize + let "_" := Score {1 / 9}:R in + Sample {exp_bernoulli [{10 / 11}:R]}]. + +Definition prog5 : @exp R _ [::] _ := + [Normalize Sample {exp_bernoulli [{10 / 11}:R]}]. + +End game_programs. +Arguments tail1 {R}. +Arguments tail2 {R}. +Arguments guard {R g}. + +Section from_prog0_to_prog1. +Local Open Scope ring_scope. +Local Open Scope ereal_scope. +Local Open Scope lang_scope. +Context (R : realType). +Local Notation mu := lebesgue_measure. + +Let prog01_subproof + (x : mctx (untag (ctx_of (recurse Unit (recurse Nat (found "p" Real [::])))))) + U : (0 <= x.2.2.1 <= 1)%R -> + execP [let "y" := Sample {exp_binomial 3 [#{"p"}]} in + return {1}:N <= #{"y"}] x U = + execP (@tail1 R) x U. +Proof. +move=> x01. +rewrite /tail1. +(* reduce lhs *) +rewrite execP_letin execP_sample execD_binomial/= execP_return/= execD_rel/=. +rewrite exp_var'E (execD_var_erefl "p")/=. +rewrite exp_var'E (execD_var_erefl "y")/=. +rewrite execD_nat/=. +rewrite [LHS]letin'E/=. +(* reduce rhs *) +rewrite execP_sample/= execD_bernoulli/= (@execD_bin _ _ binop_minus)/=. +rewrite execD_real/= execD_pow/= (@execD_bin _ _ binop_minus)/= execD_real/=. +rewrite (execD_var_erefl "p")/=. +exact/integral_binomial_prob. +Qed. + +Lemma prog01 : execD (@prog0 R) = execD (@prog1 R). +Proof. +rewrite /prog0 /prog1. +apply: congr_normalize => y A. +apply: execP_letin_uniform => // p [] B p01. +apply: congr_letinr => a1 V0. +apply: congr_letinr => -[] V1. +exact: prog01_subproof. +Qed. + +End from_prog0_to_prog1. + +Section from_prog1_to_prog2. +Local Open Scope ring_scope. +Local Open Scope ereal_scope. +Local Open Scope lang_scope. +Context (R : realType). +Local Notation mu := lebesgue_measure. + +Let prog12_subproof (y : @mctx R [::]) (V : set (@mtyp R Bool)) + (p : R) + (x : projT2 (existT measurableType default_measure_display unit)) + (U : set (mtyp Bool)) + (p0 : (0 <= p)%R) + (p1 : (p <= 1)%R) : + \int[binomial_prob 8 p]_y0 + execP [let "_" := {guard "x" 5} in {tail1}] + (y0, (p, x)) U = + \int[mscale (NngNum (normr_ge0 (56 * XMonemX 5 3 p))) \d_tt]_y0 + execP tail2 (y0, (p, x)) U. +Proof. +rewrite integral_binomial//=. +rewrite (bigD1 (inord 5))//=. +rewrite big1 ?adde0; last first. + move=> i i5. + rewrite execP_letin/= execP_if/= execD_rel/=. + rewrite exp_var'E/= (execD_var_erefl "x")/=. + rewrite execD_nat/= execP_score/= execD_real/= execP_return/=. + rewrite letin'E iteE/=. + move: i => [[|[|[|[|[|[|[|[|[|//]]]]]]]]]]//= Hi in i5 *; + rewrite ?ge0_integral_mscale//= ?execD_real/= ?normr0 ?(mul0e,mule0)//. + by rewrite -val_eqE/= inordK in i5. +(* reduce lhs *) +rewrite -[(p ^+ _ * _ ^+ _)%R]/(XMonemX _ _ p). +rewrite execP_letin/= execP_if/= execD_rel/=. +rewrite exp_var'E/= (execD_var_erefl "x")/=. +rewrite execD_nat/= execP_score/= execD_real/= execP_return/=. +rewrite letin'E iteE/=. +rewrite inordK// eqxx. +rewrite integral_dirac//= execD_unit/= diracE mem_set// mul1e. +(* reduce rhs *) +rewrite ge0_integral_mscale//=. +rewrite integral_dirac//= diracE mem_set// mul1e. +rewrite ger0_norm ?mulr_ge0 ?subr_ge0//. +rewrite mulr_natl. +(* same score *) +congr *%E. +(* the tails are the same module the shape of the environment *) +rewrite /tail1 /tail2 !execP_sample/=. +rewrite !execD_bernoulli/=. +rewrite !(@execD_bin _ _ binop_minus)/=. +rewrite !execD_pow/=. +rewrite !execD_real/=. +rewrite !(@execD_bin _ _ binop_minus)/=. +by rewrite !execD_real/= !exp_var'E/= !(execD_var_erefl "p")/=. +Qed. + +Lemma prog12 : execD (@prog1 R) = execD (@prog2 R). +Proof. +apply: congr_normalize => y V. +apply: execP_letin_uniform => // p x U /andP[p0 p1]. +(* reduce the lhs *) +rewrite execP_letin execP_sample execD_binomial/=. +rewrite letin'E/=. +rewrite [in LHS]exp_var'E/= (execD_var_erefl "p")/=. +(* reduce the rhs *) +rewrite [in RHS]execP_letin execP_score/=. +rewrite letin'E/=. +do 2 rewrite (@execD_bin _ _ binop_mult)/=/=. +rewrite [in RHS]exp_var'E/=. +rewrite execD_pow/=. +rewrite (execD_var_erefl "p")/=. +rewrite execD_pow/=. +rewrite (@execD_bin _ _ binop_minus)/=/=. +rewrite 2!execD_real/=. +rewrite (execD_var_erefl "p")/=. +rewrite -(mulrA 56). +exact: prog12_subproof. +Qed. + +End from_prog1_to_prog2. + +Local Open Scope ereal_scope. + +Lemma measurable_bernoulli_onemXn {R : realType} U : + measurable_fun [set: g_sigma_algebraType R.-ocitv.-measurable] + (fun x => bernoulli (1 - `1-x ^+ 3) U). +Proof. +apply: (measurableT_comp (measurable_bernoulli2 _)) => //=. +apply: measurable_funB => //. +by apply: measurable_funX; exact: measurable_funB. +Qed. + +Lemma bounded_norm_XnonemXn {R : realType} : + [bounded normr (56 * XMonemX 5 3 x)%R : R^o | x in `[0%R, 1%R] : set R]. +Proof. exact/(bounded_norm _).1/boundedMl/bounded_XMonemX. Qed. + +Lemma integrable_bernoulli_XMonemX {R : realType} U : + (beta_prob 1 1).-integrable [set: R] + (fun x => bernoulli (1 - `1-x ^+ 3) U * (normr (56 * XMonemX 5 3 x))%:E). +Proof. +apply/integrableP; split. + apply: emeasurable_funM; first exact: measurable_bernoulli_onemXn. + apply/measurable_EFinP => //; apply: measurableT_comp => //. + by apply: measurable_funM => //; exact: measurable_fun_XMonemX. +rewrite beta_prob_uniform integral_uniform//=. + rewrite subr0 invr1 mul1e. + suff : lebesgue_measure.-integrable `[0%R, 1%R] + (fun y : R => bernoulli (1 - `1-y ^+ 3) U * (normr (56 * XMonemX 5 3 y))%:E). + by move=> /integrableP[]. + apply: integrableMl => //=. + - apply/integrableP; split. + by apply: measurable_funTS; exact: measurable_bernoulli_onemXn. + have := @integral_beta_prob_bernoulli_onem_lty R 3 1%N 1%N U. + rewrite beta_prob_uniform integral_uniform//=; last first. + by apply: measurableT_comp => //=; exact: measurable_bernoulli_onemXn. + by rewrite subr0 invr1 mul1e. + - apply: @measurableT_comp => //=; apply: measurable_funM => //. + exact: measurable_fun_XMonemX. + exact: bounded_norm_XnonemXn. +apply: @measurableT_comp => //; apply: emeasurable_funM => //=. + exact: measurable_bernoulli_onemXn. +do 2 apply: @measurableT_comp => //=. +by apply: measurable_funM => //; exact: measurable_fun_XMonemX. +Qed. + +Section from_prog2_to_prog3. +Local Open Scope ring_scope. +Local Open Scope ereal_scope. +Local Open Scope lang_scope. +Context (R : realType). +Local Notation mu := lebesgue_measure. + +Lemma prog22' : execD (@prog2 R) = execD (@prog2' R). +Proof. +apply: congr_normalize => // x U. +apply: congr_letinl => // y V. +rewrite !execP_sample execD_uniform/= execD_beta/=. +by rewrite beta_prob_uniform. +Qed. + +Lemma prog23 : execD (@prog2' R) = execD (@prog3 R). +Proof. +apply: congr_normalize => x U. +(* reduce the LHS *) +rewrite 2![in LHS]execP_letin. +rewrite ![in LHS]execP_sample. +rewrite [in LHS]execP_score. +rewrite [in LHS]execD_beta/=. +rewrite [in LHS]execD_bernoulli. +rewrite 2![in LHS](@execD_bin _ _ binop_mult)/=. +rewrite 2![in LHS]execD_pow/=. +rewrite 2![in LHS](@execD_bin _ _ binop_minus)/=. +rewrite 3![in LHS]execD_real. +rewrite [in LHS]exp_var'E [in LHS](execD_var_erefl "p")/=. +rewrite [in LHS]execD_pow/=. +rewrite [in LHS](@execD_bin _ _ binop_minus)/=. +rewrite [in LHS]execD_real. +rewrite [in LHS]exp_var'E [in LHS](execD_var_erefl "p")/=. +(* reduce the RHS *) +rewrite [in RHS]execP_letin. +rewrite [in RHS]execP_score. +rewrite [in RHS]execP_letin/=. +rewrite [in RHS]execP_sample/=. +rewrite [in RHS]execD_beta/=. +rewrite [in RHS]execP_sample/=. +rewrite [in RHS]execD_bernoulli/=. +rewrite [in RHS]execD_real/=. +rewrite [in RHS](@execD_bin _ _ binop_minus)/=. +rewrite [in RHS]execD_real/=. +rewrite [in RHS]execD_pow/=. +rewrite [in RHS](@execD_bin _ _ binop_minus)/=. +rewrite [in RHS]exp_var'E [in RHS](execD_var_erefl "p")/=. +rewrite [in RHS]execD_real/=. +rewrite [LHS]letin'E/=. +under eq_integral => y _. + rewrite letin'E/=. + rewrite integral_cst//= /mscale/= diracT mule1 -mulrA -/(XMonemX _ _ _). + over. +rewrite [RHS]letin'E/=. +under [in RHS]eq_integral => y _. + rewrite letin'E/=. + over. +rewrite /=. +rewrite [RHS]ge0_integral_mscale//=; last first. + by move=> _ _; rewrite integral_ge0. +rewrite integral_beta_prob//=; last 2 first. + - apply: emeasurable_funM => //=. + exact: measurable_bernoulli_onemXn. + apply/measurable_EFinP; apply: measurableT_comp => //. + by apply: measurable_funM => //; exact: measurable_fun_XMonemX. + - by have /integrableP[] := @integrable_bernoulli_XMonemX R U. +rewrite ger0_norm// integral_dirac// diracT mul1e. +rewrite integral_beta_prob/=; [|by []|exact: measurable_bernoulli_onemXn + |exact: integral_beta_prob_bernoulli_onem_lty]. +rewrite -integralZl//=; last exact: integrable_bernoulli_beta_pdf. +apply: eq_integral => y _. +rewrite [in RHS]muleCA -[in LHS]muleA; congr *%E. +rewrite /beta_pdf /XMonemX01 2!patchE; case: ifPn => [y01|_]; last first. + by rewrite !mul0r 2!mule0. +rewrite ger0_norm; last first. + by rewrite mulr_ge0// XMonemX_ge0//; rewrite inE in y01. +rewrite [X in _ = _ * X]EFinM [in RHS]muleCA. +rewrite /= XMonemX00 mul1r [in LHS](mulrC 56) [in LHS]EFinM -[in LHS]muleA; congr *%E. +by rewrite !betafunE/= !factE/= -EFinM; congr EFin; lra. + +Qed. + +End from_prog2_to_prog3. + +Local Open Scope ereal_scope. +(* TODO: move? *) +Lemma int_beta_prob01 {R : realType} (f : R -> R) a b U : + measurable_fun [set: R] f -> + (forall x, x \in `[0%R, 1%R] -> 0 <= f x <= 1)%R -> + \int[beta_prob a b]_y bernoulli (f y) U = + \int[beta_prob a b]_(y in `[0%R, 1%R] : set R) bernoulli (f y) U. +Proof. +move=> mf f01. +rewrite [LHS]integral_beta_prob//=; last 2 first. + apply: measurable_funTS. + by apply: (measurableT_comp (measurable_bernoulli2 _)) => //=. + exact: integral_beta_prob_bernoulli_lty. +rewrite [RHS]integral_beta_prob//; last 2 first. + apply/measurable_funTS => //=. + by apply: (measurableT_comp (measurable_bernoulli2 _)) => //=. + apply: (le_lt_trans _ (lang_syntax.integral_beta_prob_bernoulli_lty a b U mf f01)). + apply: ge0_subset_integral => //=; apply: measurableT_comp => //=. + by apply: (measurableT_comp (measurable_bernoulli2 _)) => //=. +rewrite [RHS]integral_mkcond/=; apply: eq_integral => x _ /=. +rewrite !patchE; case: ifPn => // x01. +by rewrite /beta_pdf /XMonemX01 patchE (negbTE x01) mul0r mule0. +Qed. + +Lemma expr_onem_01 {R : realType} (x : R) : x \in `[0%R, 1%R] -> + (0 <= `1-x ^+ 3 <= 1)%R. +Proof. +rewrite in_itv/= => /andP[x0 x1]. +rewrite exprn_ge0 ?subr_ge0//= exprn_ile1// ?subr_ge0//. +by rewrite lerBlDl -lerBlDr subrr. +Qed. + +Lemma int_beta_prob_bernoulli {R : realType} (U : set (@mtyp R Bool)) : + \int[beta_prob 6 4]_y bernoulli (`1-y ^+ 3) U = bernoulli (1 / 11) U :> \bar R. +Proof. +rewrite int_beta_prob01//; last 2 first. + by apply: measurable_funX => //; exact: measurable_funB. + exact: expr_onem_01. +have := @beta_prob_bernoulliE R 6 4 0 3 U isT isT. +rewrite /beta_prob_bernoulli. +under eq_integral. + move=> x x0. + rewrite /XMonemX01 patchE x0 XMonemX0. + over. +rewrite /= => ->; congr bernoulli. +by rewrite /div_betafun addn0 !betafunE/= !factE/=; field. +Qed. + +Lemma dirac_bool {R : realType} (U : set bool) : + \d_false U + \d_true U = (\sum_(x \in U) (1%E : \bar R))%R. +Proof. +have [| | |] := set_bool U => /eqP ->; rewrite !diracE. +- by rewrite memNset// mem_set//= fsbig_set1 add0e. +- by rewrite mem_set// memNset//= fsbig_set1 adde0. +- by rewrite !in_set0 fsbig_set0 adde0. +- rewrite !in_setT setT_bool fsbigU0//=; last by move=> x [->]. + by rewrite !fsbig_set1. +Qed. + +Lemma int_beta_prob_bernoulli_onem {R : realType} (U : set (@mtyp R Bool)) : + \int[beta_prob 6 4]_y bernoulli (`1-(`1-y ^+ 3)) U = bernoulli (10 / 11) U :> \bar R. +Proof. +transitivity (\d_false U + \d_true U - bernoulli (1 / 11) U : \bar R)%E; last first. + rewrite /bernoulli ifT; last lra. + rewrite ifT; last lra. + apply/eqP; rewrite sube_eq//; last first. + rewrite ge0_adde_def// inE. + by apply/sume_ge0 => //= b _; rewrite lee_fin bernoulli_pmf_ge0//; lra. + by apply/sume_ge0 => //= b _; rewrite lee_fin bernoulli_pmf_ge0//; lra. + rewrite -fsbig_split//=. + under eq_fsbigr. + move=> /= x _. + rewrite -EFinD /bernoulli_pmf [X in X%:E](_ : _ = 1%R); last first. + case: x => //; lra. + over. + by rewrite /= dirac_bool. +rewrite -int_beta_prob_bernoulli. +apply/esym/eqP; rewrite sube_eq//; last first. + by rewrite ge0_adde_def// inE; exact: integral_ge0. +rewrite int_beta_prob01; last 2 first. + apply: measurable_funB => //; apply: measurable_funX => //. + exact: measurable_funB. + move=> x x01. + by rewrite subr_ge0 andbC lerBlDr -lerBlDl subrr expr_onem_01. +rewrite [X in _ == _ + X]int_beta_prob01; last 2 first. + by apply: measurable_funX => //; exact: measurable_funB. + exact: expr_onem_01. +rewrite -ge0_integralD//=; last 2 first. + apply: (@measurableT_comp _ _ _ _ _ _ (bernoulli ^~ U)) => /=. + exact: measurable_bernoulli2. + apply: measurable_funB => //=; apply: measurable_funX => //=. + exact: measurable_funB. + apply: (@measurableT_comp _ _ _ _ _ _ (bernoulli ^~ U)) => /=. + exact: measurable_bernoulli2. + by apply: measurable_funX => //=; exact: measurable_funB. +apply/eqP; transitivity + (\int[beta_prob 6 4]_(x in `[0%R, 1%R]) (\d_false U + \d_true U) : \bar R). + by rewrite integral_cst//= beta_prob01 mule1 EFinD. +apply: eq_integral => /= x x01. +rewrite /bernoulli subr_ge0 lerBlDr -lerBlDl subrr andbC. +rewrite (_ : (_ <= _ <= _)%R); last first. + by apply: expr_onem_01; rewrite inE in x01. +rewrite -fsbig_split//=. +under eq_fsbigr. + move=> /= y yU. + rewrite -EFinD /bernoulli_pmf. + rewrite [X in X%:E](_ : _ = 1%R); last first. + by case: ifPn => _; rewrite subrK. + over. +by rewrite /= dirac_bool. +Qed. + +Local Close Scope ereal_scope. + +Section from_prog3_to_prog4. +Local Open Scope ereal_scope. +Local Open Scope lang_scope. +Context (R : realType). + +(* NB: not used *) +Lemma prog34' U : + @execP R [::] _ [let "p" := Sample {exp_beta 6 4} in + Sample {exp_bernoulli [{[{1}:R - #{"p"}]} ^+ {3%N}]}] tt U = + @execP R [::] _ [Sample {exp_bernoulli [{1 / 11}:R]}] tt U. +Proof. +(* reduce the lhs *) +rewrite execP_letin. +rewrite execP_sample execD_beta/=. +rewrite execP_sample/= execD_bernoulli/=. +rewrite execD_pow/= (@execD_bin _ _ binop_minus) execD_real/=. +rewrite exp_var'E (execD_var_erefl "p")/=. +(* reduce the rhs *) +rewrite execP_sample execD_bernoulli/= execD_real. +(* semantics of lhs *) +rewrite letin'E/=. +exact: int_beta_prob_bernoulli. +Qed. + +Lemma prog34 l u U : + @execP R l _ [let "p" := Sample {exp_beta 6 4} in + Sample {exp_bernoulli [{1}:R - {[{1}:R - #{"p"}]} ^+ {3%N}]}] u U = + @execP R l _ [Sample {exp_bernoulli [{10 / 11}:R]}] u U. +Proof. +(* reduce the lhs *) +rewrite execP_letin. +rewrite execP_sample execD_beta/=. +rewrite execP_sample/= execD_bernoulli/=. +rewrite (@execD_bin _ _ binop_minus)/=. +rewrite execD_pow/= (@execD_bin _ _ binop_minus) execD_real/=. +rewrite exp_var'E (execD_var_erefl "p")/=. +(* reduce the rhs *) +rewrite execP_sample execD_bernoulli/= execD_real. +(* semantics of lhs *) +rewrite letin'E/=. +exact: int_beta_prob_bernoulli_onem. +Qed. + +End from_prog3_to_prog4. + +Section from_prog4_to_prog5. +Local Open Scope ring_scope. +Local Open Scope ereal_scope. +Local Open Scope lang_scope. +Context (R : realType). +Local Notation mu := lebesgue_measure. + +Lemma normalize_score_bernoulli g p q (p0 : (0 < p)%R) (q01 : (0 <= q <= 1)%R) : + @execD R g _ [Normalize let "_" := Score {p}:R in + Sample {exp_bernoulli [{q}:R]}] = + execD [Normalize Sample {exp_bernoulli [{q}:R]}]. +Proof. +apply: eq_execD. +rewrite !execD_normalize_pt/= !execP_letin !execP_score. +rewrite !execP_sample !execD_bernoulli !execD_real/=. +apply: funext=> x. +apply: eq_probability=> /= U. +rewrite !normalizeE/=. +rewrite !bernoulliE//=; [|lra..]. +rewrite !diracT !mule1 -EFinD add_onemK onee_eq0/=. +rewrite !letin'E. +under eq_integral. + move=> A _ /=. + rewrite !bernoulliE//=; [|lra..]. + rewrite !diracT !mule1 -EFinD add_onemK. + over. +rewrite !ge0_integral_mscale//= (ger0_norm (ltW p0))//. +rewrite integral_dirac// !diracT !indicT /= !mule1 !mulr1. +rewrite add_onemK invr1 mule1. +rewrite gt_eqF ?lte_fin//=. +rewrite integral_dirac//= diracT mul1e. +by rewrite muleAC -EFinM divff ?gt_eqF// mul1e bernoulliE. +Qed. + +Lemma prog45 : execD (@prog4 R) = execD (@prog5 R). +Proof. by rewrite normalize_score_bernoulli//; lra. Qed. + +End from_prog4_to_prog5. + +Lemma from_prog0_to_prog5 {R : realType} : execD (@prog0 R) = execD (@prog5 R). +Proof. +rewrite prog01 prog12 prog22' prog23. +rewrite -prog45. +apply: congr_normalize => y V. +apply: congr_letinr => x U. +by rewrite -prog34. +Qed. diff --git a/theories/lebesgue_integral.v b/theories/lebesgue_integral.v index 284f18536..5ebcac3f8 100644 --- a/theories/lebesgue_integral.v +++ b/theories/lebesgue_integral.v @@ -7196,6 +7196,18 @@ Qed. End nicely_shrinking. +Section set_itv_porderType. +Variables (d : Order.disp_t) (T : porderType d). +Implicit Types (x y : T). + +Lemma subset_itv' x y z u : (x < y)%O -> (z < u)%O -> `[y, z] `<=` `]x, u[. +Proof. +move=> xy zu w/=; rewrite !in_itv/= => /andP[yw wz]. +by rewrite (lt_le_trans xy)//= (le_lt_trans wz). +Qed. + +End set_itv_porderType. + Section nice_lebesgue_differentiation. Local Open Scope ereal_scope. Context {R : realType}. diff --git a/theories/lebesgue_measure.v b/theories/lebesgue_measure.v index 9611908ef..5ebbbcdfb 100644 --- a/theories/lebesgue_measure.v +++ b/theories/lebesgue_measure.v @@ -1221,7 +1221,7 @@ Lemma measurable_itv_bnd_infty b x : Proof. case: b; first by apply: sub_sigma_algebra; exists x; rewrite set_itv_c_infty. rewrite [X in measurable X](_ : _ = - \bigcup_(k in [set k | k >= x]%N) `[k.+1, +oo[%classic); last first. + \bigcup_(k in [set k | k >= x]%N) `[k.+1, +oo[%classic); last first. apply/seteqP; split => [z /=|/= z [t/= xt]]. rewrite in_itv/= andbT => xz; exists z.-1 => /=. by rewrite -ltnS//=; case: z xz. @@ -1363,7 +1363,7 @@ Lemma measurable_fun_leq D f g : measurable_fun D f -> measurable_fun D g -> Proof. move=> mf mg mD Y mY; have [| | |] := set_bool Y => /eqP ->. - rewrite preimage_true [X in _ `&` X](_ : _ = - \bigcup_(i in range g) ([set y | f y <= i]%O `&` [set t | i <= g t]%O)). + \bigcup_(i in range g) ([set y | f y <= i]%O `&` [set t | i <= g t]%O)). rewrite setI_bigcupr; apply: bigcup_measurable => k fk. rewrite setIIr; apply: measurableI => //. + by rewrite -preimage_itvNyc; exact: mf. diff --git a/theories/measure.v b/theories/measure.v index c41343aa7..0a3b817a0 100644 --- a/theories/measure.v +++ b/theories/measure.v @@ -1550,8 +1550,8 @@ Lemma measurable_fun_if (g h : T1 -> T2) D (mD : measurable D) measurable_fun D (fun t => if f t then g t else h t). Proof. move=> mx my /= _ B mB; rewrite (_ : _ @^-1` B = - ((f @^-1` [set true]) `&` (g @^-1` B)) `|` - ((f @^-1` [set false]) `&` (h @^-1` B))). + ((f @^-1` [set true]) `&` (g @^-1` B)) `|` + ((f @^-1` [set false]) `&` (h @^-1` B))). rewrite setIUr; apply: measurableU. - by rewrite setIA; apply: mx => //; exact: mf. - by rewrite setIA; apply: my => //; exact: mf. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 1015136a0..d6c2c03e6 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -6,38 +6,39 @@ From mathcomp Require Import mathcomp_extra boolp classical_sets. From mathcomp Require Import functions cardinality fsbigop. From mathcomp Require Import reals ereal signed topology normedtype sequences. From mathcomp Require Import esum measure lebesgue_measure numfun. -From mathcomp Require Import lebesgue_integral exp kernel charge. +From mathcomp Require Import lebesgue_integral probability exp kernel charge. From mathcomp Require Import ring lra. (**md**************************************************************************) (* # Semantics of a probabilistic programming language using s-finite kernels *) -(* bernoulli p1 == Bernoulli probability with p1 a proof that *) -(* p : {nonneg R} is smaller than 1 *) -(* bernoulli_trunc r == Bernoulli probability with real number r *) -(* bino_term n k p == $\binom{n}{k}p^k (1-p)^(n-k)$ *) -(* Computes a binomial distribution term for *) -(* k successes in n trials with success rate p *) -(* binomial_probability n p1 == binomial probability with n and p1 a proof *) -(* that p : {nonneg R} is smaller than 1 *) -(* binomial_probability_trunc n r == binomial probability with n and real *) -(* number r *) -(* uniform_probability a b ab0 == uniform probability over the interval [a,b] *) -(* sample mP == sample according to the probability P where mP is a *) -(* proof that P is a measurable function *) -(* letin l k == execute l, augment the context, and execute k *) -(* ret mf == access the context with f and return the result *) -(* score mf == observe t from d, where f is the density of d and *) -(* t occurs in f *) -(* e.g., score (r e^(-r * t)) = observe t from exp(r) *) -(* normalize k P == normalize the kernel k into a probability kernel, *) -(* P is a default probability in case normalization is *) -(* not possible *) -(* ite mf k1 k2 == access the context with the boolean function f and *) -(* behaves as k1 or k2 according to the result *) (* *) -(* poisson == Poisson distribution function *) -(* exp_density == density function for exponential distribution *) +(* Reference: *) +(* - R. Affeldt, C. Cohen, A. Saito. Semantics of probabilistic programs *) +(* using s-finite kernels in Coq. CPP 2023 *) (* *) +(* ``` *) +(* poisson_pdf == Poisson pdf *) +(* exponential_pdf == exponential distribution pdf *) +(* *) +(* sample mP == sample according to the probability P where mP is *) +(* a proof that P is a measurable function *) +(* sample_cst P == sample according to the probability P *) +(* letin l k == execute l, augment the context, and execute k *) +(* ret mf == access the context with f and return the result *) +(* score mf == observe t from d, where f is the density of d and *) +(* t occurs in f *) +(* e.g., score (r e^(-r * t)) = observe t from exp(r) *) +(* normalize k P == normalize the kernel k into a probability kernel, *) +(* P is a default probability in case normalization *) +(* is not possible *) +(* ite mf k1 k2 == access the context with the boolean function f and *) +(* behaves as k1 or k2 according to the result *) +(* case_nat == case analysis on the nat datatype *) +(* *) +(* mkswap k == given a kernel k : (Y * X) ~> Z, *) +(* returns a kernel of type (X * Y) ~> Z *) +(* letin' := mkcomp \o mkswap *) +(* ``` *) (******************************************************************************) Set Implicit Arguments. @@ -50,45 +51,6 @@ Local Open Scope classical_set_scope. Local Open Scope ring_scope. Local Open Scope ereal_scope. -(* Definition mR (R : realType) : Type := R. -HB.instance Definition _ (R : realType) := Measurable.on (mR R). -(* [the measurableType (R.-ocitv.-measurable).-sigma of - salgebraType (R.-ocitv.-measurable)]. *) *) - -Module Notations. -(*Notation var1of2 := (@measurable_fst _ _ _ _). -Notation var2of2 := (@measurable_snd _ _ _ _). -Notation var1of3 := (measurableT_comp (@measurable_fst _ _ _ _) - (@measurable_fst _ _ _ _)). -Notation var2of3 := (measurableT_comp (@measurable_snd _ _ _ _) - (@measurable_fst _ _ _ _)). -Notation var3of3 := (@measurable_snd _ _ _ _).*) - -(* Definition mR R := [the measurableType (R.-ocitv.-measurable).-sigma of - salgebraType (R.-ocitv.-measurable)]. *) -Notation munit := Datatypes_unit__canonical__measure_Measurable. -Notation mbool := Datatypes_bool__canonical__measure_Measurable. -Notation mnat := Datatypes_nat__canonical__measure_Measurable. -End Notations. - -(* TODO: PR *) -Lemma onem_nonneg_proof (R : numDomainType) (p : {nonneg R}) : - (p%:num <= 1 -> 0 <= `1-(p%:num))%R. -Proof. by rewrite /onem/= subr_ge0. Qed. - -Definition onem_nonneg (R : numDomainType) (p : {nonneg R}) - (p1 : (p%:num <= 1)%R) := - NngNum (onem_nonneg_proof p1). -(* /TODO: PR *) - -Lemma invr_nonneg_proof (R : numDomainType) (p : {nonneg R}) : - (0 <= (p%:num)^-1)%R. -Proof. by rewrite invr_ge0. Qed. - -Definition invr_nonneg (R : numDomainType) (p : {nonneg R}) := - NngNum (invr_nonneg_proof p). - -(* TODO: move *) Lemma eq_probability R d (Y : measurableType d) (m1 m2 : probability Y R) : (m1 =1 m2 :> (set Y -> \bar R)) -> m1 = m2. Proof. @@ -114,249 +76,87 @@ subst p2. by f_equal. Qed. -Section constants. -Variable R : realType. -Local Open Scope ring_scope. - -Lemma onem1S n : `1- (1 / n.+1%:R) = (n%:R / n.+1%:R)%:nng%:num :> R. -Proof. -by rewrite /onem/= -{1}(@divrr _ n.+1%:R) ?unitfE// -mulrBl -natr1 addrK. -Qed. - -Lemma p1S n : (1 / n.+1%:R)%:nng%:num <= 1 :> R. -Proof. by rewrite ler_pdivrMr//= mul1r ler1n. Qed. - -Lemma p12 : (1 / 2%:R)%:nng%:num <= 1 :> R. Proof. by rewrite p1S. Qed. - -Lemma p14 : (1 / 4%:R)%:nng%:num <= 1 :> R. Proof. by rewrite p1S. Qed. - -Lemma onem27 : `1- (2 / 7%:R) = (5%:R / 7%:R)%:nng%:num :> R. -Proof. by apply/eqP; rewrite subr_eq/= -mulrDl -natrD divrr// unitfE. Qed. - -Lemma p27 : (2 / 7%:R)%:nng%:num <= 1 :> R. -Proof. by rewrite /= lter_pdivrMr// mul1r ler_nat. Qed. - -End constants. -Arguments p12 {R}. -Arguments p14 {R}. -Arguments p27 {R}. -Arguments p1S {R}. - -Section bernoulli. -Variables (R : realType) (p : {nonneg R}) (p1 : (p%:num <= 1)%R). - -Definition bernoulli : set bool -> \bar R := - measure_add - [the measure _ _ of mscale p [the measure _ _ of dirac true]] - [the measure _ _ of mscale (onem_nonneg p1) [the measure _ _ of dirac false]]. - -HB.instance Definition _ := Measure.on bernoulli. - -Let bernoulli_setT : bernoulli [set: _] = 1. -Proof. -rewrite /bernoulli/= /measure_add/= /msum 2!big_ord_recr/= big_ord0 add0e/=. -by rewrite /mscale/= !diracT !mule1 -EFinD add_onemK. -Qed. - -HB.instance Definition _ := - @Measure_isProbability.Build _ _ R bernoulli bernoulli_setT. - -End bernoulli. - -Lemma integral_bernoulli {R : realType} (p : {nonneg R}) (p1 : (p%:num <= 1)%R) - (f : bool -> \bar R) : (forall x, 0 <= f x) -> - \int[bernoulli p1]_y (f y) = - p%:num%:E * f true + (`1-(p%:num))%:E * f false. -Proof. -move=> f0. -rewrite ge0_integral_measure_sum// 2!big_ord_recl/= big_ord0 adde0/=. -by rewrite !ge0_integral_mscale//= !integral_dirac//= !diracT 2!mul1e. -Qed. +Definition dep_uncurry (A : Type) (B : A -> Type) (C : Type) : + (forall a : A, B a -> C) -> {a : A & B a} -> C := + fun f p => let (a, Ba) := p in f a Ba. -Section bernoulli_trunc. -Variables (R : realType). +Section binomial_total. Local Open Scope ring_scope. +Variables (R : realType) (n : nat). +Implicit Type p : R. -Lemma sumbool_ler (x y : R) : (x <= y)%R + (x > y)%R. -Proof. -have [_|_] := leP x y. -by apply (*left*) inl. -by apply (*right*) inr. -Qed. - -(* TODO: move? *) -Definition dep_uncurry := -(fun (A : Type) (B : A -> Type) (C : Type) (f : forall a : A, B a -> C) (p : {a : A & B a}) => let (a, Ba) := p in f a Ba) : -forall [A : Type] [B : A -> Type] [C : Type], -(forall a : A, B a -> C) -> {a : A & B a} -> C. - -Definition bernoulli0 := @bernoulli R 0%R%:nng ler01. - -HB.instance Definition _ := Probability.on bernoulli0. - -Definition bernoulli_trunc (p : R) := match sumbool_ler 0%R p with -| inl l0p => match sumbool_ler (NngNum l0p)%:num 1%R with - | inl lp1 => [the probability _ _ of @bernoulli R (NngNum l0p) lp1] - | inr _ => bernoulli0 - end -| inr _ => bernoulli0 -end. - -Lemma bernoulli_truncE (p : R) U : - (0 <= p <= 1)%R -> - (bernoulli_trunc p U = - p%:E * \d_true U + (`1-p)%:E * \d_false U)%E. -Proof. -move=> /andP[p0 p1]. -rewrite /bernoulli_trunc. -case: (sumbool_ler 0 p) => [{}p0/=|]. - case: (sumbool_ler p 1) => [{}p1/=|]. - by rewrite /bernoulli/= measure_addE. - by rewrite ltNge p1. -by rewrite ltNge p0. -Qed. - -(* HB.instance Definition _ (p : R) := Probability.on (bernoulli_trunc p). *) - -Let simpe := (@mule0 R, @adde0 R, @mule1 R, @add0e R). - -Lemma measurable_bernoulli_trunc : - measurable_fun setT (bernoulli_trunc : _ -> pprobability _ _). +Lemma measurable_binomial_prob : + measurable_fun setT (binomial_prob n : R -> pprobability _ _). Proof. apply: (@measurability _ _ _ _ _ _ (@pset _ _ _ : set (set (pprobability _ R)))) => //. move=> _ -[_ [r r01] [Ys mYs <-]] <-; apply: emeasurable_fun_infty_o => //=. -rewrite /bernoulli_trunc/=. -have := @subsetT _ Ys; rewrite setT_bool => UT. -have [->|->|->|->] /= := subset_set2 UT. -- rewrite [X in measurable_fun _ X](_ : _ = cst 0%E)//. - apply/funext => x/=. - by case: sumbool_ler. -- rewrite [X in measurable_fun _ X](_ : _ = (fun x => if 0 <= x <= 1 then x%:E else 0%E))//. - by apply: measurable_fun_ifT => //=; apply: measurable_and => //; exact: measurable_fun_ler. - apply/funext => x/=; case: sumbool_ler => /= x0. - case: sumbool_ler => /= x1. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. - by rewrite /mscale/= !diracE mem_set//= memNset//= ?simpe x0 x1. - rewrite /bernoulli0 /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. - by rewrite /mscale/= !diracE mem_set//= memNset//= ?simpe x0/= leNgt x1. - rewrite /bernoulli0 /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. - by rewrite /mscale/= !diracE mem_set//= memNset//= ?simpe leNgt x0. -- rewrite [X in measurable_fun _ X](_ : _ = - (fun x => if 0 <= x <= 1 then (`1-x)%:E else 1%E))//. - apply: measurable_fun_ifT => //=. - by apply: measurable_and => //; exact: measurable_fun_ler. - by apply/measurable_EFinP; apply/measurable_funB. - apply/funext => x/=; case: sumbool_ler => /= x0. - case: sumbool_ler => /= x1. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. - by rewrite /mscale/= !diracE memNset//= mem_set//= ?simpe x0 x1/=. - rewrite /bernoulli0 /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. - by rewrite /mscale/= !diracE memNset//= mem_set//= ?simpe x0/= leNgt x1/= onem0. - rewrite /bernoulli0. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. - by rewrite /mscale/= !diracE memNset//= mem_set//= leNgt x0/= ?simpe onem0. -- rewrite [X in measurable_fun _ X](_ : _ = cst 1%E)//; apply/funext => x/=. - case: sumbool_ler => /= x0. - case: sumbool_ler => /= x1. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. - rewrite /mscale/= !diracE mem_set//=; last by left. - rewrite mem_set//=; last by right. - by rewrite ?simpe -EFinD add_onemK. - rewrite /bernoulli0. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. - rewrite /mscale/= !diracE mem_set//=; last by left. - rewrite mem_set//=; last by right. - by rewrite ?simpe onem0. - rewrite /bernoulli0. - rewrite /bernoulli/= /measure_add/= /msum/= !big_ord_recl//= big_ord0//=. - rewrite /mscale/= !diracE mem_set//=; last by left. - rewrite mem_set//=; last by right. - by rewrite ?simpe onem0. -Qed. - -End bernoulli_trunc. - -Arguments bernoulli_trunc {R}. -Arguments measurable_bernoulli_trunc {R}. - -Lemma integral_bernoulli_trunc {R : realType} (p : R) (f : bool -> \bar R) : - (0 <= p <= 1)%R -> (forall x, 0 <= f x) -> - \int[bernoulli_trunc p]_y (f y) = p%:E * f true + (`1-p)%:E * f false. -Proof. -move=> /andP[p0 p1] f0; rewrite /bernoulli_trunc. -case: sumbool_ler => [? /=|]. - case: (sumbool_ler p 1) => [? /=|]. - by rewrite integral_bernoulli. - by rewrite ltNge p1. -by rewrite ltNge p0. -Qed. - -Section binomial_probability. -Context {R : realType} (n : nat) (p : {nonneg R}) (p1 : (p%:num <= 1)%R). +rewrite /binomial_prob/=. +set f := (X in measurable_fun _ X). +apply: measurable_fun_if => //=. + by apply: measurable_and => //; exact: measurable_fun_ler. +apply: (eq_measurable_fun (fun t => + \sum_(k x /set_mem[_/= x01]. + rewrite nneseries_esum// -1?[in RHS](set_mem_set Ys)// => k kYs. + by rewrite lee_fin binomial_pmf_ge0. +apply: ge0_emeasurable_sum. + by move=> k x/= [_ x01] _; rewrite lee_fin binomial_pmf_ge0. +move=> k Ysk; apply/measurableT_comp => //. +exact: measurable_binomial_pmf. +Qed. + +End binomial_total. +Arguments measurable_binomial_prob {R}. + +Section poisson_pdf. +Variable R : realType. Local Open Scope ring_scope. -(* C(n, k) * p^k * (1-p)^(n-k) *) -Definition bino_term (k : nat) : {nonneg R} := - (p%:num^+k * (NngNum (onem_ge0 p1))%:num^+(n-k)%N *+ 'C(n, k))%:nng. +Definition poisson_pdf k r : R := + if r > 0 then r ^+ k / k`!%:R^-1 * expR (- r) else 1%:R. -Lemma bino_term0 : - bino_term 0 = ((NngNum (onem_ge0 p1))%:num^+n)%:nng. +Lemma poisson_pdf_ge0 k r : 0 <= poisson_pdf k r. Proof. -rewrite /bino_term bin0 subn0/=. -apply/val_inj => /=. -by field. +rewrite /poisson_pdf; case: ifPn => r0//. +by rewrite mulr_ge0 ?expR_ge0// mulr_ge0// exprn_ge0 ?ltW. Qed. -Lemma bino_term1 : - bino_term 1 = (p%:num * (NngNum (onem_ge0 p1))%:num^+(n-1)%N *+ n)%:nng. +Lemma poisson_pdf_gt0 k r : 0 < r -> 0 < poisson_pdf k.+1 r. Proof. -rewrite /bino_term bin1/=. -apply/val_inj => /=. -by rewrite expr1. +move=> r0; rewrite /poisson_pdf r0 mulr_gt0 ?expR_gt0//. +by rewrite divr_gt0// ?exprn_gt0// invr_gt0 ltr0n fact_gt0. Qed. -Import Notations. - -(* Check \sum_(k < n.+1) (fun k => [the measure _ _ of mscale (bino_term k) - [the measure _ _ of \d_k]]). *) -(* \sum_(k < n.+1) (bino_coef p n k) * \d_k. *) -Definition binomial_probability : set nat -> \bar R := - @msum _ _ R (fun k => mscale (bino_term k) \d_k) n.+1. - -HB.instance Definition _ := Measure.on binomial_probability. - -Let binomial_setT : binomial_probability [set: _] = 1%:E. +Lemma measurable_poisson_pdf k : measurable_fun setT (poisson_pdf k). Proof. -rewrite /binomial_probability/msum/mscale/bino_term/=/mscale/=. -under eq_bigr do rewrite diracT mule1. -rewrite sumEFin. -under eq_bigr=> i _. - rewrite mulrC. - over. -rewrite -exprDn_comm; last by rewrite /GRing.comm mulrC. -by rewrite addrC add_onemK; congr _%:E; rewrite expr1n. +rewrite /poisson_pdf; apply: measurable_fun_if => //. + exact: measurable_fun_ltr. +by apply: measurable_funM => /=; + [exact: measurable_funM|exact: measurableT_comp]. Qed. -HB.instance Definition _ := - @Measure_isProbability.Build _ _ R binomial_probability binomial_setT. +End poisson_pdf. + +Section exponential_pdf. +Variable R : realType. +Local Open Scope ring_scope. + +Definition exponential_pdf x r : R := r * expR (- r * x). -End binomial_probability. +Lemma exponential_pdf_gt0 x r : 0 < r -> 0 < exponential_pdf x r. +Proof. by move=> r0; rewrite /exponential_pdf mulr_gt0// expR_gt0. Qed. -Section integral_binomial. -Variables (R : realType) (d : measure_display) (T : measurableType d). +Lemma exponential_pdf_ge0 x r : 0 <= r -> 0 <= exponential_pdf x r. +Proof. by move=> r0; rewrite /exponential_pdf mulr_ge0// expR_ge0. Qed. -Lemma integral_binomial (n : nat) (p : {nonneg R}) - (p1 : (p%:num <= 1)%R) (f : nat -> \bar R) - (mf : measurable_fun setT f) : - (forall x, 0 <= f x) -> \int[binomial_probability n p1]_y (f y) = - \sum_(k < n.+1) (bino_term n p1 k)%:num%:E * f k. +Lemma measurable_exponential_pdf x : measurable_fun setT (exponential_pdf x). Proof. -move=> f0; rewrite ge0_integral_measure_sum//=; apply: eq_bigr => i _. -by rewrite ge0_integral_mscale//= integral_dirac//= diracT mul1e. +apply: measurable_funM => //=; apply: measurableT_comp => //. +exact: measurable_funM. Qed. -End integral_binomial. +End exponential_pdf. (* X + Y is a measurableType if X and Y are *) HB.instance Definition _ (X Y : pointedType) := @@ -402,117 +202,61 @@ Lemma measurable_fun_if_pair_nat d d' (X : measurableType d) Proof. move=> mx my; apply: measurable_fun_ifT => //=. - have h : measurable_fun [set: nat] (fun t => t == n) by []. - exact: (@measurableT_comp _ _ _ _ _ _ _ _ _ h). + exact: (measurableT_comp h). - exact: measurableT_comp. - exact: measurableT_comp. Qed. -Section binomial_trunc. -Variables (R : realType). -Local Open Scope ring_scope. +(* Definition mR (R : realType) : Type := R. +HB.instance Definition _ (R : realType) := Measurable.on (mR R). +(* [the measurableType (R.-ocitv.-measurable).-sigma of + salgebraType (R.-ocitv.-measurable)]. *) *) + +Module Notations. +Notation munit := Datatypes_unit__canonical__measure_Measurable. +Notation mbool := Datatypes_bool__canonical__measure_Measurable. +Notation mnat := Datatypes_nat__canonical__measure_Measurable. +End Notations. -Definition binomial_probability0 := @binomial_probability R 0 0%:nng%R ler01. +Lemma invr_nonneg_proof (R : numDomainType) (p : {nonneg R}) : + (0 <= (p%:num)^-1)%R. +Proof. by rewrite invr_ge0. Qed. -Definition binomial_probability_trunc n (p : R) := - match (sumbool_ler 0%R p) with - | inl l0p => match (sumbool_ler (NngNum l0p)%:num 1%R) with - | inl lp1 => [the probability _ _ of @binomial_probability R n (NngNum l0p) lp1] - | inr _ => [the probability _ _ of binomial_probability0] - end - | inr _ => [the probability _ _ of binomial_probability0] - end. +Definition invr_nonneg (R : numDomainType) (p : {nonneg R}) := + NngNum (invr_nonneg_proof p). -Lemma measurable_binomial_probability_trunc (n : nat) - : measurable_fun setT (binomial_probability_trunc n : R -> pprobability _ _). -Proof. -apply: (@measurability _ _ _ _ _ _ - (@pset _ _ _ : set (set (pprobability _ R)))) => //. -move=> _ -[_ [r r01] [Ys mYs <-]] <-; apply: emeasurable_fun_infty_o => //=. -rewrite /binomial_probability_trunc/=. -set f := (X in measurable_fun _ X). -rewrite (_ : f = fun x => if 0 <= x <= 1 then (\sum_(m < n.+1) - match sumbool_ler 0 x with - | inl l0p => - match sumbool_ler x 1 with - | inl lp1 => mscale (@bino_term _ n (NngNum l0p) lp1 m) (\d_(nat_of_ord m)) Ys - | inr _ => (x ^+ m * `1-x ^+ (n - m) *+ 'C(n, m))%:E * \d_(nat_of_ord m) Ys - end - | inr _ => (x ^+ m * `1-x ^+ (n - m) *+ 'C(n, m))%:E * \d_(nat_of_ord m) Ys - end)%E - else \d_0%N Ys)//. - move=> ?; apply: measurable_fun_ifT => //=. - by apply: measurable_and => //; exact: measurable_fun_ler. - apply: emeasurable_sum => m /=. - rewrite /mscale/= [X in measurable_fun _ X](_ : _ = (fun x => - ((x ^+ m * `1-x ^+ (n - m) *+ 'C(n, m))%:E * \d_(nat_of_ord m) Ys)%E)); last first. - by apply:funext => x; case: sumbool_ler => // x0; case: sumbool_ler. - apply: emeasurable_funM => //; apply/measurable_EFinP => //. - under eq_fun do rewrite -mulrnAr. - apply: measurable_funM => //. - under eq_fun do rewrite -mulr_natr. - apply: measurable_funM => //=. - apply: measurable_funX. - exact: measurable_funB. -rewrite {}/f. -apply/funext => x. -case: sumbool_ler => /= x0. - case: sumbool_ler => /= x1. - by rewrite /binomial_probability/= /msum /= /bino_term/= x0 x1. - rewrite /binomial_probability /= /msum big_ord_recl/= big_ord0 /mscale. - by rewrite /= expr0 mul1r subnn expr0 bin0 mul1e adde0 !leNgt x1 andbF. -rewrite /binomial_probability /= /msum big_ord_recl/= big_ord0 /mscale. -by rewrite /= expr0 mul1r subnn expr0 bin0 mul1e adde0 !leNgt x0/=. -Qed. - -End binomial_trunc. - -Arguments binomial_probability_trunc {R}. -Arguments measurable_binomial_probability_trunc {R}. - -Section integral_binomial_trunc. -Variables (R : realType) (d : measure_display) (T : measurableType d). +Section constants. +Variable R : realType. +Local Open Scope ring_scope. -Import Notations. -Lemma integral_binomial_probabilty_trunc (n : nat) (p : R) - (p0 : (0 <= p)%R) (p1 : ((NngNum p0)%:num <= 1)%R) (f : mnat -> \bar R) - (mf : measurable_fun setT f) : (forall x, 0 <= f x) -> - \int[binomial_probability_trunc n p]_y (f y) = - \sum_(k < n.+1) (bino_term n p1 k)%:num%:E * f k. +Lemma onem1S n : `1- (1 / n.+1%:R) = (n%:R / n.+1%:R)%:nng%:num :> R. Proof. -move=> f0; rewrite /binomial_probability_trunc/=. -case: sumbool_ler => [? /=|]. - case: sumbool_ler => [?/=|]. - by rewrite integral_binomial. - by rewrite ltNge p1. -by rewrite ltNge p0. +by rewrite /onem/= -{1}(@divrr _ n.+1%:R) ?unitfE// -mulrBl -natr1 addrK. Qed. -End integral_binomial_trunc. +Lemma p1S n : (1 / n.+1%:R)%:nng%:num <= 1 :> R. +Proof. by rewrite ler_pdivrMr//= mul1r ler1n. Qed. + +Lemma p12 : (1 / 2%:R)%:nng%:num <= 1 :> R. Proof. by rewrite p1S. Qed. -Section binomial_example. -Context {R : realType}. -Open Scope ring_scope. +Lemma p14 : (1 / 4%:R)%:nng%:num <= 1 :> R. Proof. by rewrite p1S. Qed. -Lemma binomial3_2 : @binomial_probability R 3 _ (p1S 1) [set 2%N] = (3 / 8)%:E. -Proof. -rewrite /binomial_probability/msum !big_ord_recl/= big_ord0 adde0 bino_term0. -rewrite /mscale/= !diracE /bump/=. -repeat rewrite ?binS ?bin0 ?bin1 ?bin_small//. -rewrite memNset//=. -rewrite memNset//=. -rewrite mem_set//=. -rewrite memNset//=. -congr _%:E. -rewrite expr0 !mulr1 !mulr0 !add0r !addn0 !add0n /onem. -by field. -Qed. +Lemma onem27 : `1- (2 / 7%:R) = (5%:R / 7%:R)%:nng%:num :> R. +Proof. by apply/eqP; rewrite subr_eq/= -mulrDl -natrD divrr// unitfE. Qed. -End binomial_example. +(*Lemma p27 : (2 / 7%:R)%:nng%:num <= 1 :> R. +Proof. by rewrite /= lter_pdivrMr// mul1r ler_nat. Qed.*) + +End constants. +Arguments p12 {R}. +Arguments p14 {R}. +(*Arguments p27 {R}.*) +Arguments p1S {R}. Section uniform_probability. Context (R : realType) (a b : R) (ab0 : (0 < b - a)%R). -Definition uniform_probability : set _ -> \bar R := +Definition uniform_probability (* : set _ -> \bar R *) := @mscale _ _ R (invr_nonneg (NngNum (ltW ab0))) (mrestr lebesgue_measure (measurable_itv `[a, b])). @@ -586,570 +330,14 @@ apply: eq_fsbigr => r _; rewrite ge0_integralZl//. - by rewrite lee_fin invr_ge0// ltW. Qed. -Lemma integral_uniform (f : _ -> \bar R) (a b : R) - (ab0 : (0 < b - a)%R) : measurable_fun setT f -> (forall x, 0 <= f x) -> - let m := uniform_probability ab0 in - \int[m]_x f x = - (b - a)^-1%:E * \int[lebesgue_measure]_(x in `[a, b]) f x. -Proof. -move=> mf f0 m. -pose f_ := nnsfun_approx measurableT mf. -transitivity (lim (\int[m]_(x in setT) (f_ n x)%:E @[n --> \oo])). - rewrite -monotone_convergence//=. - - apply: eq_integral => ? /[!inE] xD; apply/esym/cvg_lim => //=. - exact/cvg_nnsfun_approx. - - by move=> n; exact/measurable_EFinP/measurable_funTS. - - by move=> n ? _; rewrite lee_fin. - - by move=> ? _ ? ? mn; rewrite lee_fin; exact/lefP/nd_nnsfun_approx. -rewrite [X in _ = _ * X](_ : _ = lim - (\int[lebesgue_measure]_(x in `[a, b]) (f_ n x)%:E @[n --> \oo])); last first. - rewrite -monotone_convergence//=. - - by apply: eq_integral => ? /[!inE] xD; apply/esym/cvg_lim => //; exact: cvg_nnsfun_approx. - - by move=> n; exact/measurable_EFinP/measurable_funTS. - - by move=> n ? _; rewrite lee_fin. - - by move=> ? _ ? ? ab; rewrite lee_fin; exact/lefP/nd_nnsfun_approx. -rewrite -limeMl//. - apply: congr_lim. - by apply/funext => n /=; exact: integral_uniform_nnsfun. -apply/ereal_nondecreasing_is_cvgn => ? ? ab; apply: ge0_le_integral => //=. -- by move=> ? _; rewrite lee_fin. -- exact/measurable_EFinP/measurable_funTS. -- by move=> ? _; rewrite lee_fin. -- exact/measurable_EFinP/measurable_funTS. -- by move=> ? _; rewrite lee_fin; exact/lefP/nd_nnsfun_approx. -Qed. - End integral_uniform. -(* normalization constant *) -Section beta_nat_norm. -Context {R : realType} (a b : nat). - -Definition beta_nat_norm : R := - (a.-1`!%:R * b.-1`!%:R / (a + b).-1`!%:R)%R. - -Lemma beta_nat_norm_gt0 : (0 < beta_nat_norm :> R)%R. -Proof. -by rewrite /beta_nat_norm divr_gt0// ?mulr_gt0 ?ltr0n ?fact_gt0. -Qed. - -Lemma beta_nat_norm_ge0 : (0 <= beta_nat_norm :> R)%R. -Proof. exact/ltW/beta_nat_norm_gt0. Qed. - -End beta_nat_norm. - -Lemma beta_nat_norm00 {R : realType} : beta_nat_norm 0 0 = 1%R :> R. -Proof. by rewrite /beta_nat_norm/= fact0 mulr1/= divff. Qed. - -Lemma beta_nat_norm11 {R : realType} : beta_nat_norm 1 1 = 1%R :> R. -Proof. by rewrite /beta_nat_norm/= fact0 mulr1/= divff. Qed. - -(* definition of the beta probability specialized to natural numbers *) - -Definition ubeta_nat_pdf' {R : realType} (a b : nat) (t : R) := - (t ^+ a * (`1- t) ^+ b)%R. - -Section beta_probability. -Local Open Scope ring_scope. -Context {R : realType}. -Variables a b : nat. -(* unnormalized pdf for beta specialized to nat *) - -Definition ubeta_nat_pdf (t : R) := ubeta_nat_pdf' a.-1 b.-1 t. - -Lemma ubeta_nat_pdf_ge0 t : 0 <= t <= 1 -> 0 <= ubeta_nat_pdf t. -Proof. by move=> /andP[t0 t1]; rewrite mulr_ge0// exprn_ge0// onem_ge0. Qed. - -Lemma ubeta_nat_pdf_le1 t : 0 <= t <= 1 -> ubeta_nat_pdf t <= 1. -Proof. -move=> /andP[t0 t1]; rewrite /ubeta_nat_pdf. -by rewrite mulr_ile1// ?(exprn_ge0,onem_ge0,exprn_ile1,onem_le1). -Qed. - -Lemma measurable_ubeta_nat_pdf : measurable_fun setT ubeta_nat_pdf. -Proof. -by apply /measurable_funM => //; exact/measurable_funX/measurable_funB. -Qed. - -Lemma beta_nat_normE : - (beta_nat_norm a b)%:E = (\int[lebesgue_measure]_(x in `[0%R, 1%R]) (ubeta_nat_pdf x)%:E)%E. -Proof. -rewrite /beta_nat_norm. -rewrite /ubeta_nat_pdf. -rewrite /ubeta_nat_pdf'. -Admitted. - -(* normalized pdf for beta specialized to nat *) -Definition beta_nat_pdf t := ubeta_nat_pdf t / (beta_nat_norm a b). - -Lemma measurable_beta_nat_pdf : measurable_fun setT beta_nat_pdf. -Proof. by apply: measurable_funM => //; exact: measurable_ubeta_nat_pdf. Qed. - -Lemma beta_nat_pdf_ge0 t : 0 <= t <= 1 -> 0 <= beta_nat_pdf t. -Proof. -move=> t01; rewrite /beta_nat_pdf divr_ge0//. - exact: ubeta_nat_pdf_ge0. -exact: beta_nat_norm_ge0. -Qed. - -Local Notation mu := lebesgue_measure. - -(* unnormalized beta specialized to nat *) -Definition ubeta_nat (U : set (measurableTypeR R)) : \bar R := - \int[mu]_(x in U `&` `[0, 1](*NB: is this correct?*)) (ubeta_nat_pdf x)%:E. -(* TODO: define as \int[uniform_probability p01]_(t in U) (ubeta_nat_pdf t)%:E ? *) - -Lemma ubeta_natE U : - (ubeta_nat U = - \int[mu]_(x in U `&` `[0%R, 1%R]) (ubeta_nat_pdf x)%:E :> \bar R)%E. -Proof. by []. Qed. - -Lemma ubeta_nat_lty U : (ubeta_nat U < +oo)%E. -Proof. -Admitted. - -Let ubeta_nat0 : ubeta_nat set0 = 0%:E. -Proof. by rewrite /ubeta_nat set0I integral_set0. Qed. - -Let ubeta_nat_ge0 U : (0 <= ubeta_nat U)%E. -Proof. -rewrite /ubeta_nat integral_ge0//= => x [Ux]. -by rewrite in_itv/= => x01; rewrite lee_fin ubeta_nat_pdf_ge0. -Qed. - -Let ubeta_nat_sigma_additive : semi_sigma_additive ubeta_nat. -Proof. -move=> /= F mF tF mUF; rewrite /ubeta_nat setI_bigcupl; apply: cvg_toP. - apply: ereal_nondecreasing_is_cvgn => m n mn. - apply: lee_sum_nneg_natr => // k _ _. - apply: integral_ge0 => /= x [_]; rewrite in_itv => x01. - by rewrite lee_fin; exact: ubeta_nat_pdf_ge0. -rewrite ge0_integral_bigcup//=. -- by move=> k; exact: measurableI. -- by apply/measurable_EFinP; exact: measurable_funTS measurable_ubeta_nat_pdf. -- move=> x [k _ [_]]; rewrite /= in_itv/= => x01. - by rewrite lee_fin ubeta_nat_pdf_ge0. -- exact: trivIset_setIr. -Qed. - -HB.instance Definition _ := isMeasure.Build _ _ _ ubeta_nat - ubeta_nat0 ubeta_nat_ge0 ubeta_nat_sigma_additive. - -Definition beta_nat (*: set [the measurableType (R.-ocitv.-measurable).-sigma of - salgebraType R.-ocitv.-measurable] -> \bar R*) := - @mscale _ _ _ (invr_nonneg (NngNum (beta_nat_norm_ge0 a b))) ubeta_nat. - -Let beta_nat0 : beta_nat set0 = 0. -Proof. exact: measure0. Qed. - -Let beta_nat_ge0 U : (0 <= beta_nat U)%E. -Proof. exact: measure_ge0. Qed. - -Let beta_nat_sigma_additive : semi_sigma_additive beta_nat. -Proof. move=> /= F mF tF mUF; exact: measure_semi_sigma_additive. Qed. - -HB.instance Definition _ := isMeasure.Build _ _ _ beta_nat - beta_nat0 beta_nat_ge0 beta_nat_sigma_additive. - -Let beta_nat_setT : beta_nat setT = 1%:E. -Proof. -rewrite /beta_nat /= /mscale /=. -rewrite /ubeta_nat/= setTI. -by rewrite -beta_nat_normE -EFinM mulVr// unitfE gt_eqF// beta_nat_norm_gt0. -Qed. - -HB.instance Definition _ := @Measure_isProbability.Build _ _ _ - beta_nat beta_nat_setT. - -Lemma beta_nat01 : beta_nat `[0, 1] = 1%:E. -Proof. -rewrite /beta_nat /= /mscale/=. -rewrite /beta_nat /ubeta_nat setIidr//. -by rewrite -beta_nat_normE -EFinM mulVr// unitfE gt_eqF// beta_nat_norm_gt0. -Qed. - -End beta_probability. - -Arguments beta_nat {R}. - -Section beta_probability11. -Local Open Scope ring_scope. -Context {R : realType}. - -Lemma ubeta_nat_pdf11 : ubeta_nat_pdf 1 1 = @cst R _ 1. -Proof. by apply/funext => r; rewrite /ubeta_nat_pdf/= /ubeta_nat_pdf' !expr0 mulr1. Qed. - -Let a01 : 0 < 1 - 0 :> R. Proof. by []. Qed. - -Lemma beta11_uniform U : measurable U -> - beta_nat 1 1 U = uniform_probability a01 U. -Proof. -move=> mU; rewrite /beta_nat /uniform_probability. -rewrite /mscale/= beta_nat_norm11 subr0 invr1 !mul1e. -rewrite /ubeta_nat /mrestr/=. -rewrite ubeta_nat_pdf11/= integral_cst/= ?mul1e//. -exact: measurableI. -Qed. - -End beta_probability11. - -Lemma factD n m : (n`! * m`! <= (n + m).+1`!)%N. -Proof. -elim: n m => /= [m|n ih m]. - by rewrite fact0 mul1n add0n factS leq_pmull. -rewrite 2!factS [in X in (_ <= _ * X)%N]addSn -mulnA leq_mul//. -by rewrite ltnS addSnnS leq_addr. -Qed. - -Lemma factD' n m : (n`! * m.-1`! <= (n + m)`!)%N. -Proof. -case: m => //= [|m]. - by rewrite fact0 muln1 addn0. -by rewrite addnS factD. -Qed. - -Lemma leq_prod2 (x y n m : nat) : (n <= x)%N -> (m <= y)%N -> - (\prod_(m <= i < y) i * \prod_(n <= i < x) i <= \prod_(n + m <= i < x + y) i)%N. -Proof. -move=> nx my. -rewrite big_addn. -rewrite -addnBA//. -rewrite {3}/index_iota. -rewrite -addnBAC//. -rewrite iotaD. -rewrite big_cat/=. -rewrite mulnC. -rewrite leq_mul//. - rewrite /index_iota. - apply: leq_prod. - by move=> i _; rewrite leq_addr. -rewrite subnKC//. -rewrite -{1}(add0n m). -rewrite big_addn. -rewrite {2}(_ : (y - m) = ((y - m + x) - x))%N; last first. - by rewrite -addnBA// subnn addn0. -rewrite -{1}(add0n x). -rewrite big_addn. -rewrite -addnBA// subnn addn0. -apply: leq_prod => i _. -by rewrite leq_add2r leq_addr. -Qed. - -Lemma leq_fact2 (x y n m : nat) : - (n <= x) %N -> (m <= y)%N -> - (x`! * y`! * ((n + m).+1)`! <= n`! * m`! * ((x + y).+1)`!)%N. -Proof. -move=> nx my. -rewrite (_ : x`! = n`! * \prod_(n.+1 <= i < x.+1) i)%N; last first. - by rewrite -fact_split. -rewrite -!mulnA leq_mul2l; apply/orP; right. -rewrite (_ : y`! = m`! * \prod_(m.+1 <= i < y.+1) i)%N; last first. - by rewrite -fact_split. -rewrite mulnCA -!mulnA leq_mul2l; apply/orP; right. -rewrite (_ : (x + y).+1`! = (n + m).+1`! * \prod_((n + m).+2 <= i < (x + y).+2) i)%N; last first. - rewrite -fact_split//. - by rewrite ltnS leq_add. -rewrite mulnA mulnC leq_mul2l; apply/orP; right. -rewrite -addSn -addnS. -rewrite -addSn -addnS. -exact: leq_prod2. -Qed. - -Section integral_beta. -Context {R : realType}. -Variables a b a' b' : nat. - -Local Notation mu := lebesgue_measure. - -Lemma integralMl f g1 g2 A : -measurable A -> measurable_fun A f -> - measurable_fun A g1 -> measurable_fun A g2 -> -(ae_eq mu A g1 (EFin \o g2)) -> - \int[mu]_(x in A) (f x * g1 x) = - \int[mu]_(x in A) (f x * (g2 x)%:E) :> \bar R. -Proof. -move=> mA mf mg1 mg2 Hg. -apply: ae_eq_integral => //. - by apply: emeasurable_funM. - apply: emeasurable_funM => //. - by apply/measurable_EFinP. -by apply: ae_eq_mul2l. -Qed. - -Let beta_nat_dom : (@beta_nat R a b `<< mu). -Proof. -move=> A mA muA0. -rewrite /beta_nat /mscale/= /ubeta_nat. -have -> : \int[mu]_(x0 in A `&` `[0%R, 1%R]) (ubeta_nat_pdf a b x0)%:E = 0%:E. - apply/eqP; rewrite eq_le. - apply/andP; split; last first. - apply: integral_ge0 => x [Ax /=]. - rewrite in_itv /= => x01. - by rewrite lee_fin ubeta_nat_pdf_ge0. - apply: le_trans. - apply: (@ge0_subset_integral _ _ _ _ _ A). - by apply: measurableI. - by []. - apply/measurable_EFinP. - apply: (@measurable_funS _ _ _ _ setT) => //=. - apply: measurable_ubeta_nat_pdf. - move=> x Ax. - have : (`[0%R, 1%R]%classic x). - admit. - rewrite /= in_itv/=. - apply: ubeta_nat_pdf_ge0. - apply: subIsetl. - rewrite /=. - (* rewrite integral_abs_eq0. *) (* without abs *) - admit. -by rewrite mule0. -Admitted. - -Lemma integral_beta_nat f : - measurable_fun setT f -> - \int[beta_nat a b]_(x in `[0%R, 1%R]) `|f x| < +oo -> - \int[beta_nat a b]_(x in `[0%R, 1%R]) f x = - \int[mu]_(x in `[0%R, 1%R]) (f x * (beta_nat_pdf a b x)%:E) :> \bar R. -Proof. -move=> mf finf. -rewrite -(Radon_Nikodym_change_of_variables beta_nat_dom) //=. -apply: integralMl => //. - apply: (@measurable_funS _ _ _ _ [set: R]) => //. - apply: (@measurable_funS _ _ _ _ [set: R]) => //. - rewrite Radon_NikodymE. - by exact: beta_nat_dom. - move=> /= H. - case: cid => /= h [h1 h2 h3]. - have : (measurable_fun setT h /\ \int[mu]_x `|h x| < +oo). - apply/integrableP/h2. - move=> /= [mh _]. - apply: mh. - apply: (@measurable_funS _ _ _ _ [set: R]) => //. - apply: measurable_beta_nat_pdf. - rewrite Radon_NikodymE => /= A. - by exact: beta_nat_dom. -case: cid => /= h [h1 h2 h3]. -apply: integral_ae_eq => //. - apply: integrableS h2 => //. (* integrableST? *) - apply: (@measurable_funS _ _ _ _ [set: R]) => //. - apply: measurableT_comp => //. - apply: measurable_beta_nat_pdf. - move=> E E01 mE. - have mB : measurable_fun E (EFin \o ubeta_nat_pdf a b). - apply: measurableT_comp => //. - apply: (@measurable_funS _ _ _ _ [set: R]) => //. - apply: measurable_ubeta_nat_pdf. - rewrite -(h3 _ mE). - rewrite /beta_nat/mscale/ubeta_nat/beta_nat_pdf/=. - under eq_integral do rewrite mulrC EFinM. - rewrite (integralZl mE). - rewrite /ubeta_nat setIidl //. - rewrite /=. - apply/integrableP; split. - by apply: mB. - under eq_integral => x x01. - rewrite gee0_abs /=. - over. - apply: ubeta_nat_pdf_ge0. - have : x \in `[0%R, 1%R]. - apply: (@subset_trans _ _ `[x,x] _ _ E01). - by rewrite set_interval.set_itv1 sub1set x01. - by rewrite /= in_itv/= lexx. - by rewrite in_itv/=. - rewrite /=. - have <- := (setIidl E01). - by rewrite -ubeta_natE ubeta_nat_lty. -apply/integrableP; split. - by apply: (@measurable_funS _ _ _ _ [set: R]). -exact: finf. -Qed. - -Local Open Scope ring_scope. - -(* TODO: `[0, 1]? *) -Definition beta_nat_bern U : \bar R := - \int[beta_nat a b]_y bernoulli_trunc (ubeta_nat_pdf a'.+1 b'.+1 y) U. - -Local Notation B := beta_nat_norm. - -Definition Baa'bb'Bab : R := (beta_nat_norm (a + a') (b + b')) / beta_nat_norm a b. - -Lemma Baa'bb'Bab_ge0 : 0 <= Baa'bb'Bab. -Proof. by rewrite /Baa'bb'Bab divr_ge0// beta_nat_norm_ge0. Qed. - -Definition Baa'bb'Bab_nneg : {nonneg R} := NngNum Baa'bb'Bab_ge0. - -Lemma Baa'bb'Bab_le1 : Baa'bb'Bab_nneg%:num <= 1. -Proof. -rewrite /Baa'bb'Bab_nneg/= /Baa'bb'Bab. -rewrite ler_pdivrMr// ?mul1r ?beta_nat_norm_gt0//. -rewrite /B /beta_nat_norm. -rewrite ler_pdivrMr ?ltr0n ?fact_gt0//. -rewrite mulrAC. -rewrite ler_pdivlMr ?ltr0n ?fact_gt0//. -rewrite -!natrM ler_nat. -case: a. - rewrite /= fact0 mul1n !add0n. - case: b => /=. - case: a' => //. - case: b' => //= m. - by rewrite fact0 !mul1n muln1. - move=> n/=. - by rewrite fact0 add0n muln1 mul1n factD'. - move=> m. - rewrite mulnC leq_mul// mulnC. - by rewrite (leq_trans (factD' _ _))// addSn addnS//= addnC. -move=> n. -rewrite addSn. -case: b. - rewrite !fact0 add0n muln1 [leqRHS]mulnC addn0/= leq_mul//. - by rewrite factD'. -move=> m. -clear a b. -rewrite [(n + a').+1.-1]/=. -rewrite [n.+1.-1]/=. -rewrite [m.+1.-1]/=. -rewrite addnS. -rewrite [(_ + m).+1.-1]/=. -rewrite (addSn m b'). -rewrite [(m + _).+1.-1]/=. -rewrite (addSn (n + a')). -rewrite [_.+1.-1]/=. -rewrite addSn addnS. -by rewrite leq_fact2// leq_addr. -Qed. - -Lemma onem_Baa'bb'Bab_ge0 : 0 <= 1 - (Baa'bb'Bab_nneg%:num). -Proof. by rewrite subr_ge0 Baa'bb'Bab_le1. Qed. - -Lemma onem_Baa'bb'Bab_ge0_fix : 0 <= B a b * (1 - Baa'bb'Bab_nneg%:num). -Proof. -rewrite mulr_ge0//. - rewrite /B. - exact: beta_nat_norm_ge0. -rewrite subr_ge0. -exact: Baa'bb'Bab_le1. -Qed. - -Lemma ubeta_nat_pdf_ge0' t : 0 <= ubeta_nat_pdf a'.+1 b'.+1 t :> R. -Proof. -apply: ubeta_nat_pdf_ge0. (* TODO: needs 0 <= t <= 1 *) -Admitted. - -Lemma ubeta_nat_pdf_le1' t : (NngNum (ubeta_nat_pdf_ge0' t))%:num <= 1 :> R. -Proof. -rewrite /=. -rewrite /ubeta_nat_pdf. -rewrite /ubeta_nat_pdf'. (* TODO: needs 0 <= t <= 1 *) -Admitted. - -Lemma integral_ubeta_nat' : - (\int[ubeta_nat a b]_x (ubeta_nat_pdf a'.+1 b'.+1 x)%:E = - \int[mu]_(x in `[0%R, 1%R]) - (x ^+ a'.-1 * `1-x ^+ b'.-1 * x ^+ a * `1-x ^+ b)%:E :> \bar R)%E. -Proof. -rewrite /ubeta_nat/ubeta_nat_pdf. -Admitted. - -Lemma beta_nat_bern_bern U : - (a > 0)%N -> (b > 0)%N -> - beta_nat_bern U = - bernoulli_trunc Baa'bb'Bab U. -Proof. -rewrite /beta_nat_bern. -transitivity ((\int[beta_nat a b]_(y in `[0%R, 1%R]) - bernoulli_trunc (ubeta_nat_pdf a'.+1 b'.+1 y) U)%E : \bar R). - admit. -rewrite integral_beta_nat /=; last 2 first. - apply: (@measurableT_comp _ _ _ _ _ _ (bernoulli_trunc ^~ U)). - apply: (measurability (ErealGenInftyO.measurableE R)) => //=. - move=> /= _ [_ [x ->] <-]; apply: measurableI => //. - admit. - exact: measurable_ubeta_nat_pdf. - admit. -under eq_integral => x. - rewrite inE/= in_itv/= => x01. - rewrite bernoulli_truncE. - over. - apply/andP; split. - apply/ubeta_nat_pdf_ge0/x01. - apply/ubeta_nat_pdf_le1/x01. -rewrite /=. -rewrite bernoulli_truncE; last first. - apply/andP; split. - exact: Baa'bb'Bab_ge0. - exact: Baa'bb'Bab_le1. -under eq_integral => x _. - rewrite muleC muleDr//. - over. -rewrite integralD//=; last 2 first. - (* TODO: integrableM *) - admit. - admit. -congr (_ + _). - under eq_integral do rewrite muleA muleC. - rewrite integralZl//=; last first. - admit. - rewrite muleC. - congr (_ * _)%E. - rewrite /beta_nat_pdf. - under eq_integral do rewrite EFinM -muleA muleC -muleA. - rewrite integralZl//=; last first. - admit. - transitivity (((beta_nat_norm a b)^-1)%:E * \int[lebesgue_measure]_(x in `[0%R, 1%R]) ((ubeta_nat_pdf (a+a') (b+b') x)%:E) : \bar R)%E. - congr (_ * _)%E. - apply: eq_integral => x x01. - rewrite /ubeta_nat_pdf /ubeta_nat_pdf' muleC /onem -EFinM/=. - rewrite mulrCA -mulrA -exprD mulrA -exprD. - congr (_ ^+ _ * _ ^+ _)%:E. - rewrite -!subn1 subDnCA//. - rewrite addnC -!subn1 subDnCA//. - rewrite -beta_nat_normE. - rewrite /Baa'bb'Bab/B -!EFinM. - congr _%:E. - rewrite mulrC//. -under eq_integral do rewrite muleA muleC. -rewrite integralZl//=; last first. - admit. -rewrite muleC. -congr (_ * _)%E. -rewrite /beta_nat_pdf. -under eq_integral do rewrite EFinM -muleA muleC -muleA. -rewrite integralZl//=; last first. - admit. -transitivity (((beta_nat_norm a b)^-1)%:E * \int[lebesgue_measure]_(x in `[0%R, 1%R]) ((ubeta_nat_pdf a b x)%:E - (ubeta_nat_pdf (a+a') (b+b') x)%:E) : \bar R)%E. - congr (_ * _)%E. - apply: eq_integral => x x01. - rewrite /onem -EFinM mulrBl mul1r EFinB. - congr (_ - _)%E. - rewrite /ubeta_nat_pdf /ubeta_nat_pdf'/=. - rewrite mulrCA -mulrA -exprD mulrA -exprD. - congr (_ ^+ _ * _ ^+ _)%:E. - rewrite addnC -!subn1 subDnCA//. - rewrite -!subn1 subDnCA//. -rewrite integralB_EFin//=; last 2 first. - admit. - admit. -rewrite -!beta_nat_normE -EFinM mulrBr /onem mulVf; last first. - rewrite /B mulf_eq0 negb_or. - apply/andP; split. - rewrite mulf_eq0 negb_or. - rewrite gt_eqF ?ltr0n ?fact_gt0//=. - rewrite gt_eqF ?ltr0n ?fact_gt0//=. - rewrite invr_eq0 gt_eqF ?ltr0n ?fact_gt0//=. -congr (_ - _)%:E. -by rewrite mulrC. -Admitted. - -End integral_beta. - Section mscore. Context d (T : measurableType d) (R : realType). Variable f : T -> R. -Definition mscore t : {measure set _ -> \bar R} := - let p := NngNum (normr_ge0 (f t)) in - [the measure _ _ of mscale p [the measure _ _ of dirac tt]]. +Definition mscore t : {measure set unit -> \bar R} := + let p := NngNum (normr_ge0 (f t)) in mscale p \d_tt. Lemma mscoreE t U : mscore t U = if U == set0 then 0 else `| (f t)%:E |. Proof. @@ -1174,7 +362,7 @@ Section score. Context d (T : measurableType d) (R : realType). Variable f : T -> R. -Definition k (mf : measurable_fun setT f) i t U := +Definition k (mf : measurable_fun [set: T] f) i t U := if i%:R%:E <= mscore f t U < i.+1%:R%:E then mscore f t U else @@ -1317,7 +505,7 @@ Section kiteT. Variable k : R.-ker X ~> Y. Definition kiteT : X * bool -> {measure set Y -> \bar R} := - fun xb => if xb.2 then k xb.1 else [the measure _ _ of mzero]. + fun xb => if xb.2 then k xb.1 else mzero. Let measurable_fun_kiteT U : measurable U -> measurable_fun setT (kiteT ^~ U). Proof. @@ -1373,7 +561,7 @@ Section kiteF. Variable k : R.-ker X ~> Y. Definition kiteF : X * bool -> {measure set Y -> \bar R} := - fun xb => if ~~ xb.2 then k xb.1 else [the measure _ _ of mzero]. + fun xb => if ~~ xb.2 then k xb.1 else mzero. Let measurable_fun_kiteF U : measurable U -> measurable_fun setT (kiteF ^~ U). Proof. @@ -1437,7 +625,7 @@ Variables (f : T -> bool) (u1 u2 : R.-sfker T ~> T'). Definition mite (mf : measurable_fun setT f) : T -> set T' -> \bar R := fun t => if f t then u1 t else u2 t. -Variables mf : measurable_fun setT f. +Hypothesis mf : measurable_fun [set: T] f. Let mite0 t : mite mf t set0 = 0. Proof. by rewrite /mite; case: ifPn. Qed. @@ -1459,23 +647,20 @@ Import ITE. Definition kite : R.-sfker T ~> T' := kdirac mf \; kadd (kiteT u1) (kiteF u2). *) -Definition kite := - [the R.-sfker _ ~> _ of kdirac mf] \; - [the R.-sfker _ ~> _ of kadd - [the R.-sfker _ ~> T' of kiteT u1] - [the R.-sfker _ ~> T' of kiteF u2] ]. +Definition kite : R.-sfker T ~> T' := + kdirac mf \; kadd (kiteT u1) (kiteF u2). End ite. Section insn2. Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). -Definition ret (f : X -> Y) (mf : measurable_fun setT f) - : R.-pker X ~> Y := [the R.-pker _ ~> _ of kdirac mf]. +Definition ret (f : X -> Y) (mf : measurable_fun [set: X] f) + : R.-pker X ~> Y := kdirac mf. -Definition sample (P : X -> pprobability Y R) (mP : measurable_fun setT P) +Definition sample (P : X -> pprobability Y R) (mP : measurable_fun [set: X] P) : R.-pker X ~> Y := - [the R.-pker _ ~> _ of kprobability mP]. + kprobability mP. Definition sample_cst (P : pprobability Y R) : R.-pker X ~> Y := sample (measurable_cst P). @@ -1575,7 +760,7 @@ Context d d' d3 (X : measurableType d) (Y : measurableType d') (Z : measurableType d3) (R : realType). Lemma letin_kret (k : R.-sfker X ~> Y) - (f : X * Y -> Z) (mf : measurable_fun setT f) x U : + (f : X * Y -> Z) (mf : measurable_fun [set: X * Y] f) x U : measurable U -> letin k (ret mf) x U = k x (curry f x @^-1` U). Proof. @@ -1608,9 +793,9 @@ End insn1. Section hard_constraint. Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). -Definition fail := - letin (score (@measurable_cst _ _ X _ setT (0%R : R))) - (ret (@measurable_cst _ _ _ Y setT point)). +Definition fail : R.-sfker X ~> Y := + letin (score (measurable_cst (0%R : R))) + (ret (measurable_cst point)). Lemma failE x U : fail x U = 0. Proof. by rewrite /fail letinE ge0_integral_mscale//= normr0 mul0e. Qed. @@ -1638,15 +823,13 @@ Arguments kb {d T}. Arguments kn {d T}. Section iter_mprod. -Import Notations. +Local Open Scope type_scope. -Fixpoint iter_mprod (l : list {d & measurableType d}) - : {d & measurableType d} := +Fixpoint iter_mprod (l : seq {d & measurableType d}) : {d & measurableType d} := match l with - | [::] => existT measurableType _ munit + | [::] => existT measurableType _ unit | h :: t => let t' := iter_mprod t in - existT _ _ [the measurableType (projT1 h, projT1 t').-prod of - (projT2 h * projT2 t')%type] + existT _ _ [the measurableType _ of projT2 h * projT2 t'] end. End iter_mprod. @@ -1655,15 +838,13 @@ Section acc. Import Notations. Context {R : realType}. -Fixpoint acc (l : seq {d & measurableType d}) n : - projT2 (iter_mprod l) -> projT2 (nth (existT _ _ munit) l n) := - match l return - projT2 (iter_mprod l) -> projT2 (nth (existT _ _ munit) l n) - with - | [::] => match n with | O => id | m.+1 => id end - | _ :: _ => match n with +Fixpoint acc (l : seq {d & measurableType d}) k : + projT2 (iter_mprod l) -> projT2 (nth (existT _ _ munit) l k) := + match l with + | [::] => match k with O => id | _ => id end + | _ :: _ => match k with | O => fst - | m.+1 => fun H => acc m H.2 + | m.+1 => fun x => acc m x.2 end end. @@ -1680,25 +861,22 @@ Section rpair_pairA. Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2). -Definition rpair d (T : measurableType d) t : - ([the measurableType _ of T0] -> [the measurableType _ of T0 * T])%type := +Definition rpair d (T : measurableType d) t : T0 -> T0 * T := fun x => (x, t). Lemma mrpair d (T : measurableType d) t : measurable_fun setT (@rpair _ T t). Proof. exact: measurable_fun_prod. Qed. -Definition pairA : ([the measurableType _ of T0 * T1 * T2] -> - [the measurableType _ of T0 * (T1 * T2)])%type := +Definition pairA : T0 * T1 * T2 -> T0 * (T1 * T2) := fun x => (x.1.1, (x.1.2, x.2)). -Definition mpairA : measurable_fun setT pairA. +Definition mpairA : measurable_fun [set: (T0 * T1) * T2] pairA. Proof. apply: measurable_fun_prod => /=; first exact: measurableT_comp. by apply: measurable_fun_prod => //=; exact: measurableT_comp. Qed. -Definition pairAi : ([the measurableType _ of T0 * (T1 * T2)] -> - [the measurableType _ of T0 * T1 * T2])%type := +Definition pairAi : T0 * (T1 * T2) -> T0 * T1 * T2 := fun x => (x.1, x.2.1, x.2.2). Definition mpairAi : measurable_fun setT pairAi. @@ -1723,39 +901,33 @@ Import Notations. Context d0 d1 d2 d3 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) (T3 : measurableType d3) (R : realType). -Definition pairAr d (T : measurableType d) t : - ([the measurableType _ of T0 * T1] -> - [the measurableType _ of T0 * (T1 * T)])%type := +Definition pairAr d (T : measurableType d) t : T0 * T1 -> T0 * (T1 * T) := pairA \o rpair T t. Arguments pairAr {d} T. Lemma mpairAr d (T : measurableType d) t : measurable_fun setT (pairAr T t). Proof. exact: measurableT_comp. Qed. -Definition pairAAr : ([the measurableType _ of T0 * T1 * T2] -> - [the measurableType _ of T0 * (T1 * (T2 * munit))])%type := - pairA \o pairA \o rpair munit tt. +Definition pairAAr : T0 * T1 * T2 -> T0 * (T1 * (T2 * unit)) := + pairA \o pairA \o rpair unit tt. Lemma mpairAAr : measurable_fun setT pairAAr. Proof. by do 2 apply: measurableT_comp => //. Qed. -Definition pairAAAr : ([the measurableType _ of T0 * T1 * T2 * T3] -> - [the measurableType _ of T0 * (T1 * (T2 * (T3 * munit)))])%type := - pairA \o pairA \o pairA \o rpair munit tt. +Definition pairAAAr : T0 * T1 * T2 * T3 -> T0 * (T1 * (T2 * (T3 * unit))) := + pairA \o pairA \o pairA \o rpair unit tt. Lemma mpairAAAr : measurable_fun setT pairAAAr. Proof. by do 3 apply: measurableT_comp => //. Qed. -Definition pairAArAi : ([the measurableType _ of T0 * (T1 * T2)] -> - [the measurableType _ of T0 * (T1 * (T2 * munit))])%type := +Definition pairAArAi : T0 * (T1 * T2) -> T0 * (T1 * (T2 * unit)) := pairAAr \o pairAi. Lemma mpairAArAi : measurable_fun setT pairAArAi. Proof. by apply: measurableT_comp => //=; exact: mpairAAr. Qed. -Definition pairAAArAAi : ([the measurableType _ of T3 * (T0 * (T1 * T2))] -> - [the measurableType _ of T3 * (T0 * (T1 * (T2 * munit)))])%type := - pairA \o pairA \o pairA \o rpair munit tt \o pairAi \o pairAi. +Definition pairAAArAAi : T3 * (T0 * (T1 * T2)) -> T3 * (T0 * (T1 * (T2 * unit))) := + pairA \o pairA \o pairA \o rpair unit tt \o pairAi \o pairAi. Lemma mpairAAARAAAi : measurable_fun setT pairAAArAAi. Proof. by do 5 apply: measurableT_comp => //=. Qed. @@ -1772,92 +944,91 @@ Import Notations. Context d0 d1 d2 d3 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) (T3 : measurableType d3) (R : realType). -Definition Of2 := [:: existT _ _ T0; existT _ _ T1]. +Definition T01 : seq {d & measurableType d} := [:: existT _ _ T0; existT _ _ T1]. -Definition acc0of2 : [the measurableType _ of (T0 * T1)%type] -> T0 := - @acc Of2 0 \o pairAr munit tt. +Definition acc0of2 : T0 * T1 -> T0 := + acc T01 0 \o pairAr unit tt. Lemma macc0of2 : measurable_fun setT acc0of2. Proof. -by apply: measurableT_comp; [exact: (measurable_acc Of2 0)|exact: mpairAr]. +by apply: measurableT_comp; [exact: (measurable_acc T01 0)|exact: mpairAr]. Qed. -Definition acc1of2 : [the measurableType _ of (T0 * T1)%type] -> T1 := - acc Of2 1 \o pairAr munit tt. +Definition acc1of2 : T0 * T1 -> T1 := + acc T01 1 \o pairAr unit tt. Lemma macc1of2 : measurable_fun setT acc1of2. Proof. -by apply: measurableT_comp; [exact: (measurable_acc Of2 1)|exact: mpairAr]. +by apply: measurableT_comp; [exact: (measurable_acc T01 1)|exact: mpairAr]. Qed. -Definition Of3 := [:: existT _ _ T0; existT _ _ T1; existT _ d2 T2]. +Definition T02 := [:: existT _ _ T0; existT _ _ T1; existT _ _ T2]. -Definition acc1of3 : [the measurableType _ of (T0 * T1 * T2)%type] -> T1 := - acc Of3 1 \o pairAAr. +Definition acc1of3 : T0 * T1 * T2 -> T1 := + acc T02 1 \o pairAAr. Lemma macc1of3 : measurable_fun setT acc1of3. Proof. -by apply: measurableT_comp; [exact: (measurable_acc Of3 1)|exact: mpairAAr]. +by apply: measurableT_comp; [exact: (measurable_acc T02 1)|exact: mpairAAr]. Qed. -Definition acc2of3 : [the measurableType _ of (T0 * T1 * T2)%type] -> T2 := - acc Of3 2 \o pairAAr. +Definition acc2of3 : T0 * T1 * T2 -> T2 := + acc T02 2 \o pairAAr. Lemma macc2of3 : measurable_fun setT acc2of3. Proof. -by apply: measurableT_comp; [exact: (measurable_acc Of3 2)|exact: mpairAAr]. +by apply: measurableT_comp; [exact: (measurable_acc T02 2)|exact: mpairAAr]. Qed. -Definition acc0of3' : [the measurableType _ of (T0 * (T1 * T2))%type] -> T0 := - acc Of3 0 \o pairAArAi. +Definition acc0of3' : T0 * (T1 * T2) -> T0 := + acc T02 0 \o pairAArAi. Lemma macc0of3' : measurable_fun setT acc0of3'. Proof. -by apply: measurableT_comp; [exact: (measurable_acc Of3 0)|exact: mpairAArAi]. +by apply: measurableT_comp; [exact: (measurable_acc T02 0)|exact: mpairAArAi]. Qed. -Definition acc1of3' : [the measurableType _ of (T0 * (T1 * T2))%type] -> T1 := - acc Of3 1 \o pairAArAi. +Definition acc1of3' : T0 * (T1 * T2) -> T1 := + acc T02 1 \o pairAArAi. Lemma macc1of3' : measurable_fun setT acc1of3'. Proof. -by apply: measurableT_comp; [exact: (measurable_acc Of3 1)|exact: mpairAArAi]. +by apply: measurableT_comp; [exact: (measurable_acc T02 1)|exact: mpairAArAi]. Qed. -Definition acc2of3' : [the measurableType _ of (T0 * (T1 * T2))%type] -> T2 := - acc Of3 2 \o pairAArAi. +Definition acc2of3' : T0 * (T1 * T2) -> T2 := + acc T02 2 \o pairAArAi. Lemma macc2of3' : measurable_fun setT acc2of3'. Proof. -by apply: measurableT_comp; [exact: (measurable_acc Of3 2)|exact: mpairAArAi]. +by apply: measurableT_comp; [exact: (measurable_acc T02 2)|exact: mpairAArAi]. Qed. -Definition Of4 := +Definition T03 := [:: existT _ _ T0; existT _ _ T1; existT _ d2 T2; existT _ d3 T3]. -Definition acc1of4 : [the measurableType _ of (T0 * T1 * T2 * T3)%type] -> T1 := - acc Of4 1 \o pairAAAr. +Definition acc1of4 : T0 * T1 * T2 * T3 -> T1 := + acc T03 1 \o pairAAAr. Lemma macc1of4 : measurable_fun setT acc1of4. Proof. -by apply: measurableT_comp; [exact: (measurable_acc Of4 1)|exact: mpairAAAr]. +by apply: measurableT_comp; [exact: (measurable_acc T03 1)|exact: mpairAAAr]. Qed. -Definition acc2of4' : - [the measurableType _ of (T0 * (T1 * (T2 * T3)))%type] -> T2 := - acc Of4 2 \o pairAAArAAi. +Definition acc2of4' : T0 * (T1 * (T2 * T3)) -> T2 := + acc T03 2 \o pairAAArAAi. Lemma macc2of4' : measurable_fun setT acc2of4'. Proof. -by apply: measurableT_comp; [exact: (measurable_acc Of4 2)|exact: mpairAAARAAAi]. +by apply: measurableT_comp; [exact: (measurable_acc T03 2)|exact: mpairAAARAAAi]. Qed. -Definition acc3of4 : [the measurableType _ of (T0 * T1 * T2 * T3)%type] -> T3 := - acc Of4 3 \o pairAAAr. +Definition acc3of4 : T0 * T1 * T2 * T3 -> T3 := + acc T03 3 \o pairAAAr. Lemma macc3of4 : measurable_fun setT acc3of4. Proof. -by apply: measurableT_comp; [exact: (measurable_acc Of4 3)|exact: mpairAAAr]. +by apply: measurableT_comp; [exact: (measurable_acc T03 3)|exact: mpairAAAr]. Qed. End accessor_functions. @@ -1872,33 +1043,470 @@ Arguments macc1of4 {d0 d1 d2 d3 _ _ _ _}. Arguments macc2of4' {d0 d1 d2 d3 _ _ _ _}. Arguments macc3of4 {d0 d1 d2 d3 _ _ _ _}. -Section insn1_lemmas. -Import Notations. -Context d (T : measurableType d) (R : realType). +(* sample programs *) +Section poisson. +Variable R : realType. +Local Open Scope ring_scope. -Let kcomp_scoreE d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) - (g : R.-sfker [the measurableType _ of (T1 * unit)%type] ~> T2) - f (mf : measurable_fun setT f) r U : - (score mf \; g) r U = `|f r|%:E * g (r, tt) U. +(* density function for Poisson *) +Definition poisson k r : R := + if r > 0 then r ^+ k / k`!%:R^-1 * expR (- r) else 1%:R. + +Lemma poisson_ge0 k r : 0 <= poisson k r. Proof. -rewrite /= /kcomp /kscore /= ge0_integral_mscale//=. -by rewrite integral_dirac// diracT mul1e. +rewrite /poisson; case: ifPn => r0//. +by rewrite mulr_ge0 ?expR_ge0// mulr_ge0// exprn_ge0 ?ltW. Qed. -Lemma scoreE d' (T' : measurableType d') (x : T * T') (U : set T') (f : R -> R) - (r : R) (r0 : (0 <= r)%R) - (f0 : (forall r, 0 <= r -> 0 <= f r)%R) (mf : measurable_fun setT f) : - score (measurableT_comp mf (@macc1of2 _ _ _ _)) - (x, r) (curry (snd \o fst) x @^-1` U) = - (f r)%:E * \d_x.2 U. +Lemma poisson_gt0 k r : 0 < r -> 0 < poisson k.+1 r. Proof. -by rewrite /score/= /mscale/= ger0_norm//= f0. +move=> r0; rewrite /poisson r0 mulr_gt0 ?expR_gt0//. +by rewrite divr_gt0// ?exprn_gt0// invr_gt0 ltr0n fact_gt0. Qed. -Lemma score_score (f : R -> R) (g : R * unit -> R) - (mf : measurable_fun setT f) - (mg : measurable_fun setT g) : - letin (score mf) (score mg) = +Lemma measurable_poisson k : measurable_fun setT (poisson k). +Proof. +rewrite /poisson; apply: measurable_fun_if => //. + exact: measurable_fun_ltr. +by apply: measurable_funM => /=; + [exact: measurable_funM|exact: measurableT_comp]. +Qed. + +Definition poisson3 := poisson 4 3%:R. (* 0.168 *) +Definition poisson10 := poisson 4 10%:R. (* 0.019 *) + +End poisson. + +Section exponential. +Variable R : realType. +Local Open Scope ring_scope. + +(* density function for exponential *) +Definition exp_density x r : R := r * expR (- r * x). + +Lemma exp_density_gt0 x r : 0 < r -> 0 < exp_density x r. +Proof. by move=> r0; rewrite /exp_density mulr_gt0// expR_gt0. Qed. + +Lemma exp_density_ge0 x r : 0 <= r -> 0 <= exp_density x r. +Proof. by move=> r0; rewrite /exp_density mulr_ge0// expR_ge0. Qed. + +Lemma mexp_density x : measurable_fun setT (exp_density x). +Proof. +apply: measurable_funM => //=; apply: measurableT_comp => //. +exact: measurable_funM. +Qed. + +End exponential. + +Module CASE_NAT. +Section case_nat. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). + +Section case_nat_ker. +Variable k : R.-ker X ~> Y. + +Definition case_nat_ m (xn : X * nat) : {measure set Y -> \bar R} := + if xn.2 == m then k xn.1 else mzero. + +Let measurable_fun_case_nat_ m U : measurable U -> + measurable_fun setT (case_nat_ m ^~ U). +Proof. +move=> mU; rewrite /case_nat_ (_ : (fun _ => _) = + (fun x => if x.2 == m then k x.1 U else mzero U)) /=; last first. + by apply/funext => -[t b]/=; case: ifPn. +apply: (@measurable_fun_if_pair_nat _ _ _ _ (k ^~ U) (fun=> mzero U)) => //. +exact/measurable_kernel. +Qed. + +#[export] +HB.instance Definition _ m := isKernel.Build _ _ _ _ _ + (case_nat_ m) (measurable_fun_case_nat_ m). +End case_nat_ker. + +Section sfcase_nat. +Variable k : R.-sfker X ~> Y. + +Let sfcase_nat_ m : exists2 k_ : (R.-ker _ ~> _)^nat, + forall n, measure_fam_uub (k_ n) & + forall x U, measurable U -> case_nat_ k m x U = mseries (k_ ^~ x) 0 U. +Proof. +have [k_ hk /=] := sfinite_kernel k. +exists (fun n => case_nat_ (k_ n) m) => /=. + move=> n; have /measure_fam_uubP[r k_r] := measure_uub (k_ n). + exists r%:num => /= -[x [|n']]; rewrite /case_nat_//= /mzero//. + by case: ifPn => //= ?; rewrite /mzero. + by case: ifPn => // ?; rewrite /= /mzero. +move=> [x b] U mU; rewrite /case_nat_; case: ifPn => hb; first by rewrite hk. +by rewrite /mseries eseries0. +Qed. + +#[export] +HB.instance Definition _ m := @isSFiniteKernel_subdef.Build _ _ _ _ _ + (case_nat_ k m) (sfcase_nat_ m). +End sfcase_nat. + +Section fkcase_nat. +Variable k : R.-fker X ~> Y. + +Let case_nat_uub n : measure_fam_uub (case_nat_ k n). +Proof. +have /measure_fam_uubP[M hM] := measure_uub k. +exists M%:num => /= -[]; rewrite /case_nat_ => t [|n']/=. + by case: ifPn => //= ?; rewrite /mzero. +by case: ifPn => //= ?; rewrite /mzero. +Qed. + +#[export] +HB.instance Definition _ n := Kernel_isFinite.Build _ _ _ _ _ + (case_nat_ k n) (case_nat_uub n). +End fkcase_nat. + +End case_nat. +End CASE_NAT. + +Import CASE_NAT. + +Section case_nat. +Context d d' (T : measurableType d) (T' : measurableType d') (R : realType). + +Import CASE_NAT. + +Definition case_nat (t : R.-sfker T ~> nat) (u_ : (R.-sfker T ~> T')^nat) + : R.-sfker T ~> T' := + t \; kseries (fun n => case_nat_ (u_ n) n). + +End case_nat. + +Definition measure_sum_display : + (measure_display * measure_display) -> measure_display. +Proof. exact. Qed. + +Definition image_classes d1 d2 + (T1 : measurableType d1) (T2 : measurableType d2) (T : Type) + (f1 : T1 -> T) (f2 : T2 -> T) := + <>. + +Section sum_salgebra_instance. +Context d1 d2 (T1 : measurableType d1) (T2 : measurableType d2). +Let f1 : T1 -> T1 + T2 := @inl T1 T2. +Let f2 : T2 -> T1 + T2 := @inr T1 T2. + +Lemma sum_salgebra_set0 : image_classes f1 f2 (set0 : set (T1 + T2)). +Proof. exact: sigma_algebra0. Qed. + +Lemma sum_salgebra_setC A : image_classes f1 f2 A -> + image_classes f1 f2 (~` A). +Proof. exact: sigma_algebraC. Qed. + +Lemma sum_salgebra_bigcup (F : _^nat) : (forall i, image_classes f1 f2 (F i)) -> + image_classes f1 f2 (\bigcup_i (F i)). +Proof. exact: sigma_algebra_bigcup. Qed. + +HB.instance Definition sum_salgebra_mixin := + @isMeasurable.Build (measure_sum_display (d1, d2)) + (T1 + T2)%type (image_classes f1 f2) + sum_salgebra_set0 sum_salgebra_setC sum_salgebra_bigcup. + +End sum_salgebra_instance. +Reserved Notation "p .-sum" (at level 1, format "p .-sum"). +Reserved Notation "p .-sum.-measurable" + (at level 2, format "p .-sum.-measurable"). +Notation "p .-sum" := (measure_sum_display p) : measure_display_scope. +Notation "p .-sum.-measurable" := + ((p.-sum).-measurable : set (set (_ + _))) : + classical_set_scope. + +Module CASE_SUM. + +Section case_sum'. + +Section kcase_sum'. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). +Let A : measurableType _ := unit. +Let B : measurableType _ := bool. +Variables (k1 : A -> R.-sfker X ~> Y) (k2 : B -> R.-sfker X ~> Y). + +Definition case_sum' : X * (A + B) -> {measure set Y -> \bar R} := + fun xab => match xab with + | (x, inl a) => k1 a x + | (x, inr b) => k2 b x + end. + +Let measurable_fun_case_sum' U : measurable U -> + measurable_fun setT (case_sum' ^~ U). +Proof. +rewrite /= => mU. +apply: (measurability (ErealGenInftyO.measurableE R)) => //. +move=> /= _ [_ [x ->] <-]; apply: measurableI => //. +rewrite /case_sum'/= (_ : _ @^-1` _ = + ([set x1 | k1 tt x1 U < x%:E] `*` inl @` [set tt]) `|` + ([set x1 | k2 false x1 U < x%:E] `*` inr @` [set false]) `|` + ([set x1 | k2 true x1 U < x%:E] `*` inr @` [set true])); last first. + apply/seteqP; split. + - move=> z /=; rewrite in_itv/=; move: z => [z [[]|[|]]]//= ?. + + by do 2 left; split => //; exists tt. + + by right; split => //; exists true. + + by left; right; split => //; exists false. + - move=> z /=; rewrite in_itv/=; move: z => [z [[]|[|]]]//=. + - move=> [[[]//|]|]. + + by move=> [_ []]. + + by move=> [_ []]. + - move=> [[|]|[]//]. + + by move=> [_ []]. + + by move=> [_ [] [|]]. + - move=> [[|[]//]|]. + + by move=> [_ []]. + + by move=> [_ [] [|]]. +pose h1 := [set xub : X * (unit + bool) | k1 tt xub.1 U < x%:E]. +have mh1 : measurable h1. + rewrite -[X in measurable X]setTI; apply: emeasurable_fun_infty_o => //=. + have H : measurable_fun [set: X] (fun x => k1 tt x U) by exact/measurable_kernel. + move=> _ /= C mC; rewrite setTI. + have := H measurableT _ mC; rewrite setTI => {}H. + rewrite [X in measurable X](_ : _ = ((fun x => k1 tt x U) @^-1` C) `*` setT)//. + exact: measurableX. + by apply/seteqP; split => [z//=| z/= []]. +set h2 := [set xub : X * (unit + bool)| k2 false xub.1 U < x%:E]. +have mh2 : measurable h2. + rewrite -[X in measurable X]setTI. + apply: emeasurable_fun_infty_o => //=. + have H : measurable_fun [set: X] (fun x => k2 false x U) by exact/measurable_kernel. + move=> _ /= C mC; rewrite setTI. + have := H measurableT _ mC; rewrite setTI => {}H. + rewrite [X in measurable X](_ : _ = ((fun x => k2 false x U) @^-1` C) `*` setT)//. + exact: measurableX. + by apply/seteqP; split => [z //=|z/= []]. +set h3 := [set xub : X * (unit + bool)| k2 true xub.1 U < x%:E]. +have mh3 : measurable h3. + rewrite -[X in measurable X]setTI. + apply: emeasurable_fun_infty_o => //=. + have H : measurable_fun [set: X] (fun x => k2 (true) x U) by exact/measurable_kernel. + move=> _ /= C mC; rewrite setTI. + have := H measurableT _ mC; rewrite setTI => {}H. + rewrite [X in measurable X](_ : _ = ((fun x => k2 (true) x U) @^-1` C) `*` setT)//. + exact: measurableX. + by apply/seteqP; split=> [z//=|z/= []]. +apply: measurableU. +- apply: measurableU. + + apply: measurableX => //. + rewrite [X in measurable X](_ : _ = ysection h1 (inl tt))//. + * by apply: measurable_ysection. + * by apply/seteqP; split => z /=; rewrite /ysection /= inE. + + apply: measurableX => //. + rewrite [X in measurable X](_ : _ = ysection h2 (inr false))//. + * by apply: measurable_ysection. + * by apply/seteqP; split => z /=; rewrite /ysection /= inE. +- apply: measurableX => //. + rewrite [X in measurable X](_ : _ = ysection h3 (inr true))//. + + by apply: measurable_ysection. + + by apply/seteqP; split => z /=; rewrite /ysection /= inE. +Qed. + +#[export] +HB.instance Definition _ := isKernel.Build _ _ _ _ _ + case_sum' measurable_fun_case_sum'. +End kcase_sum'. + +Section sfkcase_sum'. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). +Let A : measurableType _ := unit. +Let B : measurableType _ := bool. +Variables (k1 : A -> R.-sfker X ~> Y) (k2 : B-> R.-sfker X ~> Y). + +Let sfinite_case_sum' : exists2 k_ : (R.-ker _ ~> _)^nat, + forall n, measure_fam_uub (k_ n) & + forall x U, measurable U -> case_sum' k1 k2 x U = mseries (k_ ^~ x) 0 U. +Proof. +rewrite /=. +set f1 : A -> (R.-fker _ ~> _)^nat := + fun ab : A => sval (cid (sfinite_kernel (k1 ab))). +set Hf1 := fun ab : A => svalP (cid (sfinite_kernel (k1 ab))). +rewrite /= in Hf1. +set f2 : B -> (R.-fker _ ~> _)^nat := + fun ab : B => sval (cid (sfinite_kernel (k2 ab))). +set Hf2 := fun ab : B => svalP (cid (sfinite_kernel (k2 ab))). +rewrite /= in Hf2. +exists (fun n => case_sum' (f1 ^~ n) (f2 ^~ n)). + move=> n /=. + have [rtt Hrtt] := measure_uub (f1 tt n). + have [rfalse Hrfalse] := measure_uub (f2 false n). + have [rtrue Hrtrue] := measure_uub (f2 true n). + exists (maxr rtt (maxr rfalse rtrue)) => //= -[x [[]|[|]]] /=. + by rewrite 2!EFin_max lt_max Hrtt. + by rewrite 2!EFin_max 2!lt_max Hrtrue 2!orbT. + by rewrite 2!EFin_max 2!lt_max Hrfalse orbT. +move=> [x [[]|[|]]] U mU/=-. +by rewrite (Hf1 tt x _ mU). +by rewrite (Hf2 true x _ mU). +by rewrite (Hf2 false x _ mU). +Qed. + +#[export] +HB.instance Definition _ := @isSFiniteKernel_subdef.Build _ _ _ _ _ + (case_sum' k1 k2) (sfinite_case_sum'). +End sfkcase_sum'. + +End case_sum'. + +Section case_sum. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). +Let A : measurableType _ := unit. +Let B : measurableType _ := bool. + +(* case analysis on the datatype unit + bool *) +Definition case_sum (f : R.-sfker X ~> (A + B)%type) + (k1 : A -> R.-sfker X ~> Y) (k2 : B -> R.-sfker X ~> Y) : R.-sfker X ~> Y := + f \; case_sum' k1 k2. + +End case_sum. + +End CASE_SUM. + +(* counting measure as a kernel *) +Section kcounting. +Context d (G : measurableType d) (R : realType). + +Definition kcounting : G -> {measure set nat -> \bar R} := fun=> counting. + +Let mkcounting U : measurable U -> measurable_fun setT (kcounting ^~ U). +Proof. by []. Qed. + +HB.instance Definition _ := isKernel.Build _ _ _ _ _ kcounting mkcounting. + +Let sfkcounting : exists2 k_ : (R.-ker _ ~> _)^nat, + forall n, measure_fam_uub (k_ n) & + forall x U, measurable U -> kcounting x U = mseries (k_ ^~ x) 0 U. +Proof. +exists (fun n => [the R.-fker _ ~> _ of + @kdirac _ _ G nat R _ (@measurable_cst _ _ _ _ setT n)]). + by move=> n /=; exact: measure_uub. +by move=> g U mU; rewrite /kcounting/= counting_dirac. +Qed. + +HB.instance Definition _ := + isSFiniteKernel_subdef.Build _ _ _ _ R kcounting sfkcounting. + +End kcounting. + +(* formalization of the iterate construct of Staton ESOP 2017, Sect. 4.2 *) +Section iterate. +Context d {G : measurableType d} {R : realType}. +Let A : measurableType _ := unit. +Let B : measurableType _ := bool. + +Import CASE_SUM. + +(* formalization of iterate^n + Gamma |-p iterate^n t from x = u : B *) +Variables (t : R.-sfker (G * A) ~> (A + B)%type) + (u : G -> A) (mu : measurable_fun setT u). + +Fixpoint iterate_ n : R.-sfker G ~> B := + match n with + | 0%N => case_sum (letin (ret mu) t) + (fun u' => fail) + (fun v => ret (measurable_cst v)) + | m.+1 => case_sum (letin (ret mu) t) + (fun u' => iterate_ m) + (fun v => fail) + end. + +(* formalization of iterate (A = unit, B = bool) + Gamma, x : A |-p t : A + B Gamma |-d u : A +----------------------------------------------- + Gamma |-p iterate t from x = u : B *) +Definition iterate : R.-sfker G ~> B := case_nat (kcounting R) iterate_. + +End iterate. + +(* an s-finite kernel to test that two expressions are different *) +Section lift_neq. +Context {R : realType} d (G : measurableType d). +Variables (f : G -> bool) (g : G -> bool). + +Definition flift_neq : G -> bool := fun x' => f x' != g x'. + +Hypotheses (mf : measurable_fun setT f) (mg : measurable_fun setT g). + +(* see also emeasurable_fun_neq *) +Lemma measurable_fun_flift_neq : measurable_fun setT flift_neq. +Proof. +apply: (@measurable_fun_bool _ _ _ _ true). +rewrite setTI. +rewrite /flift_neq /= (_ : _ @^-1` _ = ([set x | f x] `&` [set x | ~~ g x]) `|` + ([set x | ~~ f x] `&` [set x | g x])). + apply: measurableU; apply: measurableI. + - by rewrite -[X in measurable X]setTI; exact: mf. + - rewrite [X in measurable X](_ : _ = ~` [set x | g x]); last first. + by apply/seteqP; split => x /= /negP. + by apply: measurableC; rewrite -[X in measurable X]setTI; exact: mg. + - rewrite [X in measurable X](_ : _ = ~` [set x | f x]); last first. + by apply/seteqP; split => x /= /negP. + by apply: measurableC; rewrite -[X in measurable X]setTI; exact: mf. + - by rewrite -[X in measurable X]setTI; exact: mg. +by apply/seteqP; split => x /=; move: (f x) (g x) => [|] [|]//=; intuition. +Qed. + +Definition lift_neq : R.-sfker G ~> bool := ret measurable_fun_flift_neq. + +End lift_neq. + +Section von_neumann_trick. +Context d {T : measurableType d} {R : realType}. + +Definition minltt {d1 d2} {T1 : measurableType d1} {T2 : measurableType d2} := + @measurable_cst _ _ T1 _ setT (@inl unit T2 tt). + +Definition finrb d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) : + T1 * bool -> T2 + bool := fun t1b => inr t1b.2. + +Lemma minrb {d1 d2} {T1 : measurableType d1} {T2 : measurableType d2} : + measurable_fun setT (@finrb _ _ T1 T2). +Proof. exact: measurableT_comp. Qed. + +(* biased coin *) +Variable (D : pprobability bool R). + +Definition trick : R.-sfker (T * unit) ~> (unit + bool)%type := + letin (sample_cst D) + (letin (sample_cst D) + (letin (lift_neq macc1of3 macc2of3) + (ite macc3of4 + (letin (ret macc1of4) (ret minrb)) + (ret minltt)))). + +Definition von_neumann_trick : R.-sfker T ~> bool := iterate trick ktt. + +End von_neumann_trick. + +Section insn1_lemmas. +Import Notations. +Context d (T : measurableType d) (R : realType). + +Let kcomp_scoreE d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) + (g : R.-sfker [the measurableType _ of (T1 * unit)%type] ~> T2) + f (mf : measurable_fun setT f) r U : + (score mf \; g) r U = `|f r|%:E * g (r, tt) U. +Proof. +rewrite /= /kcomp /kscore /= ge0_integral_mscale//=. +by rewrite integral_dirac// diracT mul1e. +Qed. + +Lemma scoreE d' (T' : measurableType d') (x : T * T') (U : set T') (f : R -> R) + (r : R) (r0 : (0 <= r)%R) + (f0 : (forall r, 0 <= r -> 0 <= f r)%R) (mf : measurable_fun setT f) : + score (measurableT_comp mf (@macc1of2 _ _ _ _)) + (x, r) (curry (snd \o fst) x @^-1` U) = + (f r)%:E * \d_x.2 U. +Proof. +by rewrite /score/= /mscale/= ger0_norm//= f0. +Qed. + +Lemma score_score (f : R -> R) (g : R * unit -> R) + (mf : measurable_fun setT f) + (mg : measurable_fun setT g) : + letin (score mf) (score mg) = score (measurable_funM mf (measurableT_comp mg (measurable_pair2 tt))). Proof. apply/eq_sfkernel => x U. @@ -1907,16 +1515,14 @@ by rewrite kcomp_scoreE/= /mscale/= diracE normrM muleA EFinM. Qed. (* hard constraints to express score below 1 *) -Lemma score_fail (r : {nonneg R}) (r1 : (r%:num <= 1)%R) : - score (kr r%:num) = - letin (sample_cst (bernoulli r1) : R.-pker T ~> _) +Lemma score_fail (r : R) : (0 <= r <= 1)%R -> + score (kr r) = + letin (sample_cst (bernoulli r) : R.-pker T ~> _) (ite (@macc1of2 _ _ _ _) (ret ktt) fail). Proof. -apply/eq_sfkernel => x U. +move=> /andP[r0 r1]; apply/eq_sfkernel => x U. rewrite letinE/= /sample; unlock. -rewrite ge0_integral_measure_add// ge0_integral_mscale//= ge0_integral_mscale//=. -rewrite integral_dirac//= integral_dirac//= !diracT/= !mul1e. -by rewrite /mscale/= iteE//= iteE//= failE mule0 adde0 ger0_norm. +by rewrite /mscale/= ger0_norm// integral_bernoulli ?r0//= 2!iteE//= failE mule0 adde0. Qed. End insn1_lemmas. @@ -1931,17 +1537,13 @@ Variables (k1 k2 : R.-sfker T ~> Z) Lemma letin_iteT : f t -> letin (ite mf k1 k2) u t U = letin k1 u t U. Proof. -move=> ftT. -rewrite !letinE/=. -apply: eq_measure_integral => V mV _. +move=> ftT; rewrite !letinE/=; apply: eq_measure_integral => V mV _. by rewrite iteE ftT. Qed. Lemma letin_iteF : ~~ f t -> letin (ite mf k1 k2) u t U = letin k2 u t U. Proof. -move=> ftF. -rewrite !letinE/=. -apply: eq_measure_integral => V mV _. +move=> ftF; rewrite !letinE/=; apply: eq_measure_integral => V mV _. by rewrite iteE (negbTE ftF). Qed. @@ -1953,9 +1555,9 @@ Context d d' d1 d2 d3 (X : measurableType d) (Y : measurableType d') (R : realType). Import Notations. Variables (t : R.-sfker X ~> T1) - (u : R.-sfker [the measurableType _ of (X * T1)%type] ~> T2) - (v : R.-sfker [the measurableType _ of (X * T2)%type] ~> Y) - (v' : R.-sfker [the measurableType _ of (X * T1 * T2)%type] ~> Y) + (u : R.-sfker (X * T1) ~> T2) + (v : R.-sfker (X * T2) ~> Y) + (v' : R.-sfker (X * T1 * T2) ~> Y) (vv' : forall y, v =1 fun xz => v' (xz.1, y, xz.2)). Lemma letinA x A : measurable A -> @@ -1970,7 +1572,7 @@ rewrite integral_kcomp; [|by []|]. - apply: eq_integral => y _. apply: eq_integral => z _. by rewrite (vv' y). -exact: (measurableT_comp (measurable_kernel v _ mA)). +- exact: (measurableT_comp (measurable_kernel v _ mA)). Qed. End letinA. @@ -2038,71 +1640,15 @@ Qed. End letinC. -(* sample programs *) -Section poisson. -Variable R : realType. -Local Open Scope ring_scope. - -(* density function for Poisson *) -Definition poisson k r : R := - if r > 0 then r ^+ k / k`!%:R^-1 * expR (- r) else 1%:R. - -Lemma poisson_ge0 k r : 0 <= poisson k r. -Proof. -rewrite /poisson; case: ifPn => r0//. -by rewrite mulr_ge0 ?expR_ge0// mulr_ge0// exprn_ge0 ?ltW. -Qed. - -Lemma poisson_gt0 k r : 0 < r -> 0 < poisson k.+1 r. -Proof. -move=> r0; rewrite /poisson r0 mulr_gt0 ?expR_gt0//. -by rewrite divr_gt0// ?exprn_gt0// invr_gt0 ltr0n fact_gt0. -Qed. - -Lemma measurable_poisson k : measurable_fun setT (poisson k). -Proof. -rewrite /poisson; apply: measurable_fun_if => //. - exact: measurable_fun_ltr. -by apply: measurable_funM => /=; - [exact: measurable_funM|exact: measurableT_comp]. -Qed. - -Definition poisson3 := poisson 4 3%:R. (* 0.168 *) -Definition poisson10 := poisson 4 10%:R. (* 0.019 *) - -End poisson. - -Section exponential. -Variable R : realType. -Local Open Scope ring_scope. - -(* density function for exponential *) -Definition exp_density x r : R := r * expR (- r * x). - -Lemma exp_density_gt0 x r : 0 < r -> 0 < exp_density x r. -Proof. by move=> r0; rewrite /exp_density mulr_gt0// expR_gt0. Qed. - -Lemma exp_density_ge0 x r : 0 <= r -> 0 <= exp_density x r. -Proof. by move=> r0; rewrite /exp_density mulr_ge0// expR_ge0. Qed. - -Lemma mexp_density x : measurable_fun setT (exp_density x). -Proof. -apply: measurable_funM => //=; apply: measurableT_comp => //. -exact: measurable_funM. -Qed. - -End exponential. +(* examples *) Lemma letin_sample_bernoulli d d' (T : measurableType d) - (T' : measurableType d') (R : realType)(r : {nonneg R}) (r1 : (r%:num <= 1)%R) + (T' : measurableType d') (R : realType) (r : R) (u : R.-sfker [the measurableType _ of (T * bool)%type] ~> T') x y : - letin (sample_cst (bernoulli r1)) u x y = - r%:num%:E * u (x, true) y + (`1- (r%:num))%:E * u (x, false) y. -Proof. -rewrite letinE/=. -rewrite ge0_integral_measure_sum// 2!big_ord_recl/= big_ord0 adde0/=. -by rewrite !ge0_integral_mscale//= !integral_dirac//= !diracT 2!mul1e. -Qed. + (0 <= r <= 1)%R -> + letin (sample_cst (bernoulli r)) u x y = + r%:E * u (x, true) y + (`1- r)%:E * u (x, false) y. +Proof. by move=> r01; rewrite letinE/= integral_bernoulli. Qed. Section sample_and_return. Import Notations. @@ -2110,18 +1656,18 @@ Context d (T : measurableType d) (R : realType). Definition sample_and_return : R.-sfker T ~> _ := letin - (sample_cst [the probability _ _ of bernoulli p27]) (* T -> B *) + (sample_cst (bernoulli (2 / 7))) (* T -> B *) (ret macc1of2) (* T * B -> B *). Lemma sample_and_returnE t U : sample_and_return t U = (2 / 7%:R)%:E * \d_true U + (5%:R / 7%:R)%:E * \d_false U. Proof. -by rewrite /sample_and_return letin_sample_bernoulli !retE onem27. +rewrite /sample_and_return letin_sample_bernoulli; last lra. +by rewrite !retE onem27. Qed. End sample_and_return. -(* trivial example *) Section sample_and_branch. Import Notations. Context d (T : measurableType d) (R : realType). @@ -2132,14 +1678,14 @@ Context d (T : measurableType d) (R : realType). Definition sample_and_branch : R.-sfker T ~> _ := letin - (sample_cst [the probability _ _ of bernoulli p27]) (* T -> B *) + (sample_cst (bernoulli (2 / 7))) (* T -> B *) (ite macc1of2 (ret (@k3 _ _ R)) (ret k10)). Lemma sample_and_branchE t U : sample_and_branch t U = - (2 / 7%:R)%:E * \d_(3%:R : R) U + - (5%:R / 7%:R)%:E * \d_(10%:R : R) U. + (2 / 7)%:E * \d_(3 : R) U + (5 / 7)%:E * \d_(10 : R) U. Proof. -by rewrite /sample_and_branch letin_sample_bernoulli//= !iteE/= onem27. +rewrite /sample_and_branch letin_sample_bernoulli/=; last lra. +by rewrite !iteE/= onem27. Qed. End sample_and_branch. @@ -2149,43 +1695,24 @@ Context d (T : measurableType d) (R : realType). Import Notations. Definition bernoulli_and : R.-sfker T ~> mbool := - (letin (sample_cst [the probability _ _ of bernoulli p12]) - (letin (sample_cst [the probability _ _ of bernoulli p12]) + (letin (sample_cst (bernoulli (1 / 2))) + (letin (sample_cst (bernoulli (1 / 2))) (ret (measurable_and macc1of3 macc2of3)))). -(* -Definition mand (x y : T * mbool * mbool -> mbool) - (t : T * mbool * mbool) : mbool := x t && y t. - -Lemma measurable_fun_mand (x y : T * mbool * mbool -> mbool) : - measurable_fun setT x -> measurable_fun setT y -> - measurable_fun setT (mand x y). -Proof. -move=> /= mx my; apply: (measurable_fun_bool true). -rewrite [X in measurable X](_ : _ = - (x @^-1` [set true]) `&` (y @^-1` [set true])); last first. - by rewrite /mand; apply/seteqP; split => z/= /andP. -apply: measurableI. -- by rewrite -[X in measurable X]setTI; exact: mx. -- by rewrite -[X in measurable X]setTI; exact: my. -Qed. - -Definition bernoulli_and : R.-sfker T ~> mbool := - (letin (sample_cst [the probability _ _ of bernoulli p12]) - (letin (sample_cst [the probability _ _ of bernoulli p12]) - (ret (measurable_fun_mand macc1of3 macc2of3)))). -*) Lemma bernoulli_andE t U : - bernoulli_and t U = - sample_cst (bernoulli p14) t U. + bernoulli_and t U = sample_cst (bernoulli (1 / 4)) t U. Proof. -rewrite /bernoulli_and 3!letin_sample_bernoulli/= muleDr//= -muleDl//. +rewrite /bernoulli_and. +rewrite letin_sample_bernoulli; last lra. +rewrite (letin_sample_bernoulli (r := 1 / 2)); last lra. +rewrite (letin_sample_bernoulli (r := 1 / 2)); last lra. +rewrite muleDr//= -muleDl//. rewrite !muleA -addeA -muleDl// -!EFinM !onem1S/= -splitr mulr1. have -> : (1 / 2 * (1 / 2) = 1 / 4%:R :> R)%R by rewrite mulf_div mulr1// -natrM. -rewrite /bernoulli/= measure_addE/= /mscale/= -!EFinM; congr( _ + (_ * _)%:E). -have -> : (1 / 2 = 2 / 4%:R :> R)%R. - by apply/eqP; rewrite eqr_div// ?pnatr_eq0// mul1r -natrM. -by rewrite onem1S// -mulrDl. +rewrite [in RHS](_ : 1 / 4 = (1 / 4)%:nng%:num)%R//. +rewrite bernoulliE/=; last lra. +rewrite -!EFinM; congr( _ + (_ * _)%:E). +by rewrite /onem; lra. Qed. End bernoulli_and. @@ -2195,7 +1722,7 @@ Import Notations. Context d (T : measurableType d) (R : realType) (h : R -> R). Hypothesis mh : measurable_fun setT h. Definition kstaton_bus : R.-sfker T ~> mbool := - letin (sample_cst [the probability _ _ of bernoulli p27]) + letin (sample_cst (bernoulli (2 / 7))) (letin (letin (ite macc1of2 (ret k3) (ret k10)) (score (measurableT_comp mh macc2of3))) @@ -2212,43 +1739,43 @@ End staton_bus. Section staton_bus_poisson. Import Notations. Context d (T : measurableType d) (R : realType). -Let poisson4 := @poisson R 4%N. -Let mpoisson4 := @measurable_poisson R 4%N. +Let poisson4 := @poisson_pdf R 4%N. +Let mpoisson4 := @measurable_poisson_pdf R 4%N. Definition kstaton_bus_poisson : R.-sfker R ~> mbool := kstaton_bus _ mpoisson4. Let kstaton_bus_poissonE t U : kstaton_bus_poisson t U = - (2 / 7%:R)%:E * (poisson4 3%:R)%:E * \d_true U + - (5%:R / 7%:R)%:E * (poisson4 10%:R)%:E * \d_false U. + (2 / 7)%:E * (poisson4 3)%:E * \d_true U + + (5 / 7)%:E * (poisson4 10)%:E * \d_false U. Proof. -rewrite /kstaton_bus. -rewrite letin_sample_bernoulli. +rewrite /kstaton_bus_poisson /kstaton_bus. +rewrite letin_sample_bernoulli; last lra. rewrite -!muleA; congr (_ * _ + _ * _). - rewrite letin_kret//. rewrite letin_iteT//. rewrite letin_retk//. - by rewrite scoreE//= => r r0; exact: poisson_ge0. + by rewrite scoreE//= => r r0; exact: poisson_pdf_ge0. - by rewrite onem27. rewrite letin_kret//. rewrite letin_iteF//. rewrite letin_retk//. - by rewrite scoreE//= => r r0; exact: poisson_ge0. + by rewrite scoreE//= => r r0; exact: poisson_pdf_ge0. Qed. (* true -> 2/7 * 0.168 = 2/7 * 3^4 e^-3 / 4! *) (* false -> 5/7 * 0.019 = 5/7 * 10^4 e^-10 / 4! *) Lemma staton_busE P (t : R) U : - let N := ((2 / 7%:R) * poisson4 3%:R + - (5%:R / 7%:R) * poisson4 10%:R)%R in + let N := ((2 / 7) * poisson4 3 + + (5 / 7) * poisson4 10)%R in staton_bus mpoisson4 P t U = - ((2 / 7%:R)%:E * (poisson4 3%:R)%:E * \d_true U + - (5%:R / 7%:R)%:E * (poisson4 10%:R)%:E * \d_false U) * N^-1%:E. + ((2 / 7)%:E * (poisson4 3)%:E * \d_true U + + (5 / 7)%:E * (poisson4 10)%:E * \d_false U) * N^-1%:E. Proof. rewrite /staton_bus normalizeE !kstaton_bus_poissonE !diracT !mule1 ifF //. apply/negbTE; rewrite gt_eqF// lte_fin. -by rewrite addr_gt0// mulr_gt0//= ?divr_gt0// ?ltr0n// poisson_gt0// ltr0n. +by rewrite addr_gt0// mulr_gt0//= ?divr_gt0// ?ltr0n// poisson_pdf_gt0// ltr0n. Qed. End staton_bus_poisson. @@ -2260,8 +1787,8 @@ End staton_bus_poisson. Section staton_bus_exponential. Import Notations. Context d (T : measurableType d) (R : realType). -Let exp1560 := @exp_density R (ratr (15%:Q / 60%:Q)). -Let mexp1560 := @mexp_density R (ratr (15%:Q / 60%:Q)). +Let exp1560 := @exponential_pdf R (ratr (15%:Q / 60%:Q)). +Let mexp1560 := @measurable_exponential_pdf R (ratr (15%:Q / 60%:Q)). (* 15/60 = 0.25 *) @@ -2269,560 +1796,303 @@ Definition kstaton_bus_exponential : R.-sfker R ~> mbool := kstaton_bus _ mexp1560. Let kstaton_bus_exponentialE t U : kstaton_bus_exponential t U = - (2 / 7%:R)%:E * (exp1560 3%:R)%:E * \d_true U + - (5%:R / 7%:R)%:E * (exp1560 10%:R)%:E * \d_false U. + (2 / 7)%:E * (exp1560 3)%:E * \d_true U + + (5 / 7)%:E * (exp1560 10)%:E * \d_false U. Proof. rewrite /kstaton_bus. -rewrite letin_sample_bernoulli. +rewrite letin_sample_bernoulli; last lra. rewrite -!muleA; congr (_ * _ + _ * _). - rewrite letin_kret//. rewrite letin_iteT//. rewrite letin_retk//. - rewrite scoreE//= => r r0; exact: exp_density_ge0. + rewrite scoreE//= => r r0; exact: exponential_pdf_ge0. - by rewrite onem27. rewrite letin_kret//. rewrite letin_iteF//. - rewrite letin_retk//. - by rewrite scoreE//= => r r0; exact: exp_density_ge0. -Qed. - -(* true -> 5/7 * 0.019 = 5/7 * 10^4 e^-10 / 4! *) -(* false -> 2/7 * 0.168 = 2/7 * 3^4 e^-3 / 4! *) - -Lemma staton_bus_exponentialE P (t : R) U : - let N := ((2 / 7%:R) * exp1560 3%:R + - (5%:R / 7%:R) * exp1560 10%:R)%R in - staton_bus mexp1560 P t U = - ((2 / 7%:R)%:E * (exp1560 3%:R)%:E * \d_true U + - (5%:R / 7%:R)%:E * (exp1560 10%:R)%:E * \d_false U) * N^-1%:E. -Proof. -rewrite /staton_bus. -rewrite normalizeE /= !kstaton_bus_exponentialE !diracT !mule1 ifF //. -apply/negbTE; rewrite gt_eqF// lte_fin. -by rewrite addr_gt0// mulr_gt0//= ?divr_gt0// ?ltr0n// exp_density_gt0 ?ltr0n. -Qed. - -End staton_bus_exponential. - -Module CASE_NAT. -Section case_nat. -Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). - -Section case_nat_ker. -Variable k : R.-ker X ~> Y. - -Definition case_nat_ j : X * nat -> {measure set Y -> \bar R} := - fun xn => if xn.2 == j then k xn.1 else [the measure _ _ of mzero]. - -Let measurable_fun_case_nat_ m U : measurable U -> - measurable_fun setT (case_nat_ m ^~ U). -Proof. -move=> mU; rewrite /case_nat_ (_ : (fun _ => _) = - (fun x => if x.2 == m then k x.1 U else mzero U)) /=; last first. - by apply/funext => -[t b]/=; case: ifPn. -apply: (@measurable_fun_if_pair_nat _ _ _ _ (k ^~ U) (fun=> mzero U)) => //. -exact/measurable_kernel. -Qed. - -#[export] -HB.instance Definition _ j := isKernel.Build _ _ _ _ _ - (case_nat_ j) (measurable_fun_case_nat_ j). -End case_nat_ker. - -Section sfcase_nat. -Variable k : R.-sfker X ~> Y. - -Let sfcase_nat_ j : exists2 k_ : (R.-ker _ ~> _)^nat, - forall n, measure_fam_uub (k_ n) & - forall x U, measurable U -> case_nat_ k j x U = mseries (k_ ^~ x) 0 U. -Proof. -have [k_ hk /=] := sfinite_kernel k. -exists (fun n => [the _.-ker _ ~> _ of case_nat_ (k_ n) j]) => /=. - move=> n; have /measure_fam_uubP[r k_r] := measure_uub (k_ n). - exists r%:num => /= -[x [|n']]; rewrite /case_nat_//= /mzero//. - by case: ifPn => //= ?; rewrite /mzero. - by case: ifPn => // ?; rewrite /= /mzero. -move=> [x b] U mU; rewrite /case_nat_; case: ifPn => hb; first by rewrite hk. -by rewrite /mseries eseries0. -Qed. - -#[export] -HB.instance Definition _ j := @isSFiniteKernel_subdef.Build _ _ _ _ _ - (case_nat_ k j) (sfcase_nat_ j). -End sfcase_nat. - -Section fkcase_nat. -Variable k : R.-fker X ~> Y. - -Let case_nat_uub (m : nat) : measure_fam_uub (case_nat_ k m). -Proof. -have /measure_fam_uubP[M hM] := measure_uub k. -exists M%:num => /= -[]; rewrite /case_nat_ => t [|m']/=. - by case: ifPn => //= ?; rewrite /mzero//=. -by case: ifPn => //= ?; rewrite /mzero//=. -Qed. - -#[export] -HB.instance Definition _ j := Kernel_isFinite.Build _ _ _ _ _ - (case_nat_ k j) (case_nat_uub j). -End fkcase_nat. - -End case_nat. -End CASE_NAT. - -Import CASE_NAT. - -Section case_nat. -Context d d' (T : measurableType d) (T' : measurableType d') (R : realType). - -Import CASE_NAT. - -(* case analysis on the nat datatype *) -Definition case_nat (t : R.-sfker T ~> nat) (u_ : (R.-sfker T ~> T')^nat) - : R.-sfker T ~> T' := - [the R.-sfker T ~> nat of t] \; - [the R.-sfker T * nat ~> T' of - kseries (fun n => [the R.-sfker T * nat ~> T' of case_nat_ (u_ n) n])]. - -End case_nat. - -Definition measure_sum_display : - (measure_display * measure_display) -> measure_display. -Proof. exact. Qed. - -Definition image_classes d1 d2 - (T1 : measurableType d1) (T2 : measurableType d2) (T : Type) - (f1 : T1 -> T) (f2 : T2 -> T) := - <>. - -Section sum_salgebra_instance. -Context d1 d2 (T1 : measurableType d1) (T2 : measurableType d2). -Let f1 : T1 -> T1 + T2 := @inl T1 T2. -Let f2 : T2 -> T1 + T2 := @inr T1 T2. - -Lemma sum_salgebra_set0 : image_classes f1 f2 (set0 : set (T1 + T2)). -Proof. exact: sigma_algebra0. Qed. - -Lemma sum_salgebra_setC A : image_classes f1 f2 A -> - image_classes f1 f2 (~` A). -Proof. exact: sigma_algebraC. Qed. - -Lemma sum_salgebra_bigcup (F : _^nat) : (forall i, image_classes f1 f2 (F i)) -> - image_classes f1 f2 (\bigcup_i (F i)). -Proof. exact: sigma_algebra_bigcup. Qed. - -HB.instance Definition sum_salgebra_mixin := - @isMeasurable.Build (measure_sum_display (d1, d2)) - (T1 + T2)%type (image_classes f1 f2) - sum_salgebra_set0 sum_salgebra_setC sum_salgebra_bigcup. - -End sum_salgebra_instance. -Reserved Notation "p .-sum" (at level 1, format "p .-sum"). -Reserved Notation "p .-sum.-measurable" - (at level 2, format "p .-sum.-measurable"). -Notation "p .-sum" := (measure_sum_display p) : measure_display_scope. -Notation "p .-sum.-measurable" := - ((p.-sum).-measurable : set (set (_ + _))) : - classical_set_scope. - -Module CASE_SUM. - -Section case_suml. -Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). -Let A : measurableType _ := unit. - -Section kcase_suml. -Variable k : R.-ker X ~> Y. - -Definition case_suml (a : A) : X * A -> {measure set Y -> \bar R} := - fun xa => k xa.1. - -Let measurable_fun_case_suml a U : measurable U -> - measurable_fun setT (case_suml a ^~ U). -Proof. -move=> /= mU; rewrite /case_suml. -have h := measurable_kernel k _ mU. -rewrite (_ : (fun x : X * unit => k x.1 U) = (fun x : X => k x U) \o fst) //. -by apply: measurableT_comp => //. + rewrite letin_retk//. + by rewrite scoreE//= => r r0; exact: exponential_pdf_ge0. Qed. -#[export] -HB.instance Definition _ a := isKernel.Build _ _ _ _ _ - (case_suml a) (measurable_fun_case_suml a). -End kcase_suml. - -Section sfkcase_suml. -Variable k : R.-sfker X ~> Y. +(* true -> 5/7 * 0.019 = 5/7 * 10^4 e^-10 / 4! *) +(* false -> 2/7 * 0.168 = 2/7 * 3^4 e^-3 / 4! *) -Let sfinite_case_suml (a : A) : exists2 k_ : (R.-ker _ ~> _)^nat, - forall n, measure_fam_uub (k_ n) & - forall x U, measurable U -> case_suml k a x U = mseries (k_ ^~ x) 0 U. +Lemma staton_bus_exponentialE P (t : R) U : + let N := ((2 / 7) * exp1560 3 + + (5 / 7) * exp1560 10)%R in + staton_bus mexp1560 P t U = + ((2 / 7)%:E * (exp1560 3)%:E * \d_true U + + (5 / 7)%:E * (exp1560 10)%:E * \d_false U) * N^-1%:E. Proof. -have [k_ hk /=] := sfinite_kernel k. -exists (fun n => [the _.-ker _ ~> _ of case_suml (k_ n) a]) => /=. - move=> n; have /measure_fam_uubP[r k_r] := measure_uub (k_ n). - by exists r%:num => /= -[x []]; rewrite /case_suml//= /mzero//. -move=> [x b] U mU; rewrite /case_suml /=. -by rewrite /mseries hk. +rewrite /staton_bus. +rewrite normalizeE /= !kstaton_bus_exponentialE !diracT !mule1 ifF //. +apply/negbTE; rewrite gt_eqF// lte_fin. +by rewrite addr_gt0// mulr_gt0//= ?divr_gt0// ?ltr0n// exponential_pdf_gt0 ?ltr0n. Qed. -#[export] -HB.instance Definition _ (a : A) := @isSFiniteKernel_subdef.Build _ _ _ _ _ - (case_suml k a) (sfinite_case_suml a). -End sfkcase_suml. - -Section fkcase_suml. -Variable k : R.-fker X ~> Y. - -Let case_suml_uub (a : A) : measure_fam_uub (case_suml k a). -Proof. -have /measure_fam_uubP[M hM] := measure_uub k. -by exists M%:num => /= -[]; rewrite /case_suml. -Qed. +End staton_bus_exponential. -#[export] -HB.instance Definition _ a := Kernel_isFinite.Build _ _ _ _ _ - (case_suml k a) (case_suml_uub a). -End fkcase_suml. +(**md + letin' variants +*) -End case_suml. +Section mswap. +Context d d' d3 (X : measurableType d) (Y : measurableType d') + (Z : measurableType d3) (R : realType). +Variable k : R.-ker Y * X ~> Z. -Section case_sumr. -Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). -Let B : measurableType _ := bool. +Definition mswap xy U := k (swap xy) U. -Section kcase_sumr. -Variable k : R.-ker X ~> Y. +Let mswap0 xy : mswap xy set0 = 0. +Proof. done. Qed. -Definition case_sumr (b : B) : X * B -> {measure set Y -> \bar R} := - fun xa => if xa.2 == b then k xa.1 else [the measure _ _ of mzero]. +Let mswap_ge0 x U : 0 <= mswap x U. +Proof. done. Qed. -Let measurable_fun_case_sumr b U : measurable U -> - measurable_fun setT (case_sumr b ^~ U). -Proof. -move=> /= mU; rewrite /case_suml. -have h := measurable_kernel k _ mU. -rewrite /case_sumr. -rewrite (_ : (fun x : X * bool => case_sumr b x U) = - (fun x : X * bool => (if x.2 == b then k x.1 U else [the {measure set Y -> \bar R} of mzero] U))); last first. - apply/funext => x. - rewrite /case_sumr. - by case: ifPn. -apply: measurable_fun_ifT => //=. - rewrite (_ : (fun t : X * bool => t.2 == b) = (fun t : bool => t == b) \o snd)//. - apply: measurableT_comp => //. -rewrite (_ : (fun t : X * bool => k t.1 U) = (fun t : X => k t U) \o fst)//. -by apply: measurableT_comp => //. -Qed. +Let mswap_sigma_additive x : semi_sigma_additive (mswap x). +Proof. exact: measure_semi_sigma_additive. Qed. -#[export] -HB.instance Definition _ b := isKernel.Build _ _ _ _ _ - (case_sumr b) (measurable_fun_case_sumr b). -End kcase_sumr. +HB.instance Definition _ x := isMeasure.Build _ _ R + (mswap x) (mswap0 x) (mswap_ge0 x) (@mswap_sigma_additive x). -Section sfkcase_sumr. -Variable k : R.-sfker X ~> Y. +Definition mkswap : _ -> {measure set Z -> \bar R} := + fun x => mswap x. -Let sfinite_case_sumr b : exists2 k_ : (R.-ker _ ~> _)^nat, - forall n, measure_fam_uub (k_ n) & - forall x U, measurable U -> case_sumr k b x U = mseries (k_ ^~ x) 0 U. +Let measurable_fun_kswap U : + measurable U -> measurable_fun setT (mkswap ^~ U). Proof. -have [k_ hk /=] := sfinite_kernel k. -exists (fun n => [the _.-ker _ ~> _ of case_sumr (k_ n) b]) => /=. - move=> n; have /measure_fam_uubP[r k_r] := measure_uub (k_ n). - by exists r%:num => /= -[x []]; rewrite /case_sumr//=; case: ifPn => // _; - rewrite /= (le_lt_trans _ (k_r x))// /mzero//. -move=> [x [|]] U mU; rewrite /case_sumr /=; case: b => //=; rewrite ?hk//; -by rewrite /mseries/= eseries0. +move=> mU. +rewrite [X in measurable_fun _ X](_ : _ = k ^~ U \o @swap _ _)//. +apply measurableT_comp => //=; first exact: measurable_kernel. +exact: measurable_swap. Qed. -#[export] -HB.instance Definition _ b := @isSFiniteKernel_subdef.Build _ _ _ _ _ - (case_sumr k b) (sfinite_case_sumr b). -End sfkcase_sumr. +HB.instance Definition _ := isKernel.Build _ _ + (X * Y)%type Z R mkswap measurable_fun_kswap. -Section fkcase_sumr. -Variable k : R.-fker X ~> Y. +End mswap. + +Section mswap_sfinite_kernel. +Variables (d d' d3 : _) (X : measurableType d) (Y : measurableType d') + (Z : measurableType d3) (R : realType). +Variable k : R.-sfker Y * X ~> Z. -Let case_sumr_uub b : measure_fam_uub (case_sumr k b). +Let mkswap_sfinite : + exists2 k_ : (R.-ker X * Y ~> Z)^nat, + forall n, measure_fam_uub (k_ n) & + forall x U, measurable U -> mkswap k x U = kseries k_ x U. Proof. -have /measure_fam_uubP[M hM] := measure_uub k. -by exists M%:num => /= -[]; rewrite /case_sumr => x [|] /=; case: b => //=; - rewrite (le_lt_trans _ (hM x))// /mzero. +have [k_ /= kE] := sfinite_kernel k. +exists (fun n => mkswap (k_ n)). + move=> n. + have /measure_fam_uubP[M hM] := measure_uub (k_ n). + by exists M%:num => x/=; exact: hM. +move=> xy U mU. +by rewrite /mswap/= kE. Qed. -#[export] -HB.instance Definition _ b := Kernel_isFinite.Build _ _ _ _ _ - (case_sumr k b) (case_sumr_uub b). -End fkcase_sumr. - -End case_sumr. -End CASE_SUM. - -Section case_sum'. +HB.instance Definition _ := + isSFiniteKernel_subdef.Build _ _ _ Z R (mkswap k) mkswap_sfinite. -Section kcase_sum'. -Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). -Let A : measurableType _ := unit. -Let B : measurableType _ := bool. -Variables (k : (A + B)%type -> R.-sfker X ~> Y). +End mswap_sfinite_kernel. -Definition case_sum' : X * (A + B)%type -> {measure set Y -> \bar R} := - fun xab => match xab with - | (x, inl a) => CASE_SUM.case_suml (k xab.2) a (x, a) - | (x, inr b) => CASE_SUM.case_sumr (k xab.2) b (x, b) - end. +Section kswap_finite_kernel_finite. +Context d d' d3 (X : measurableType d) (Y : measurableType d') + (Z : measurableType d3) (R : realType) + (k : R.-fker Y * X ~> Z). -Let measurable_fun_case_sum' U : measurable U -> - measurable_fun setT (case_sum' ^~ U). +Let mkswap_finite : measure_fam_uub (mkswap k). Proof. -rewrite /= => mU. -apply: (measurability (ErealGenInftyO.measurableE R)) => //. -move=> /= _ [_ [x ->] <-]; apply: measurableI => //. -rewrite /case_sum' /CASE_SUM.case_suml /CASE_SUM.case_sumr /=. -rewrite (_ : - (fun x : X * (unit + bool) => (let (x0, s) := x in - match s with inl _ => k x.2 x0 | inr b => if b == b then k x.2 x0 else mzero - end) U) = - (fun x : X * (unit + bool) => k x.2 x.1 U)); last first. - by apply/funext => -[x1 [a|b]] //; rewrite eqxx. -rewrite (_ : _ @^-1` _ = - ([set x1 | k (inl tt) x1 U < x%:E] `*` inl @` [set tt]) `|` - ([set x1 | k (inr false) x1 U < x%:E] `*` inr @` [set false]) `|` - ([set x1 | k (inr true) x1 U < x%:E] `*` inr @` [set true])); last first. - apply/seteqP; split. - - move=> z /=; rewrite in_itv/=; move: z => [z [[]|[|]]]//= ?. - + by do 2 left; split => //; exists tt. - + by right; split => //; exists true. - + by left; right; split => //; exists false. - - move=> z /=; rewrite in_itv/=; move: z => [z [[]|[|]]]//=. - - move=> [[[]//|]|]. - + by move=> [_ []]. - + by move=> [_ []]. - - move=> [[|]|[]//]. - + by move=> [_ []]. - + by move=> [_ [] [|]]. - - move=> [[|[]//]|]. - + by move=> [_ []]. - + by move=> [_ [] [|]]. -pose h1 := [set xub : X * (unit + bool) | k (inl tt) xub.1 U < x%:E]. -have mh1 : measurable h1. - rewrite -[X in measurable X]setTI; apply: emeasurable_fun_infty_o => //=. - have H : measurable_fun [set: X] (fun x => k (inl tt) x U) by exact/measurable_kernel. - move=> _ /= C mC; rewrite setTI. - have := H measurableT _ mC; rewrite setTI => {}H. - rewrite [X in measurable X](_ : _ = ((fun x => k (inl tt) x U) @^-1` C) `*` setT)//. - exact: measurableX. - by apply/seteqP; split => [z//=| z/= []]. -set h2 := [set xub : X * (unit + bool)| k (inr false) xub.1 U < x%:E]. -have mh2 : measurable h2. - rewrite -[X in measurable X]setTI. - apply: emeasurable_fun_infty_o => //=. - have H : measurable_fun [set: X] (fun x => k (inr false) x U) by exact/measurable_kernel. - move=> _ /= C mC; rewrite setTI. - have := H measurableT _ mC; rewrite setTI => {}H. - rewrite [X in measurable X](_ : _ = ((fun x => k (inr false) x U) @^-1` C) `*` setT)//. - exact: measurableX. - by apply/seteqP; split => [z //=|z/= []]. -set h3 := [set xub : X * (unit + bool)| k (inr true) xub.1 U < x%:E]. -have mh3 : measurable h3. - rewrite -[X in measurable X]setTI. - apply: emeasurable_fun_infty_o => //=. - have H : measurable_fun [set: X] (fun x => k (inr true) x U) by exact/measurable_kernel. - move=> _ /= C mC; rewrite setTI. - have := H measurableT _ mC; rewrite setTI => {}H. - rewrite [X in measurable X](_ : _ = ((fun x => k (inr true) x U) @^-1` C) `*` setT)//. - exact: measurableX. - by apply/seteqP; split=> [z//=|z/= []]. -apply: measurableU. -- apply: measurableU. - + apply: measurableX => //. - rewrite [X in measurable X](_ : _ = ysection h1 (inl tt))//. - * by apply: measurable_ysection. - * by apply/seteqP; split => z /=; rewrite /ysection /= inE. - + apply: measurableX => //. - rewrite [X in measurable X](_ : _ = ysection h2 (inr false))//. - * by apply: measurable_ysection. - * by apply/seteqP; split => z /=; rewrite /ysection /= inE. -- apply: measurableX => //. - rewrite [X in measurable X](_ : _ = ysection h3 (inr true))//. - + by apply: measurable_ysection. - + by apply/seteqP; split => z /=; rewrite /ysection /= inE. +have /measure_fam_uubP[r hr] := measure_uub k. +apply/measure_fam_uubP; exists (PosNum [gt0 of r%:num%R]) => x /=. +exact: hr. Qed. -#[export] -HB.instance Definition _ := isKernel.Build _ _ _ _ _ - (case_sum') (measurable_fun_case_sum'). -End kcase_sum'. +HB.instance Definition _ := + Kernel_isFinite.Build _ _ _ Z R (mkswap k) mkswap_finite. -Section sfkcase_sum'. -Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). -Let A : measurableType _ := unit. -Let B : measurableType _ := bool. -Variables (k : (A + B)%type -> R.-sfker X ~> Y). +End kswap_finite_kernel_finite. -Let sfinite_case_sum' : exists2 k_ : (R.-ker _ ~> _)^nat, - forall n, measure_fam_uub (k_ n) & - forall x U, measurable U -> case_sum' k x U = mseries (k_ ^~ x) 0 U. -Proof. -rewrite /=. -set f : A + B -> (R.-fker _ ~> _)^nat := - fun ab : A + B => sval (cid (sfinite_kernel (k ab))). -set Hf := fun ab : A + B => svalP (cid (sfinite_kernel (k ab))). -rewrite /= in Hf. -exists (fun n => [the R.-ker _ ~> _ of case_sum' (fun ab => [the R.-fker _ ~> _ of f ab n])]). - move=> n /=. - have [rtt Hrtt] := measure_uub (f (inl tt) n). - have [rfalse Hrfalse] := measure_uub (f (inr false) n). - have [rtrue Hrtrue] := measure_uub (f (inr true) n). - exists (maxr rtt (maxr rfalse rtrue)) => //= -[x [[]|[|]]] /=. - by rewrite 2!EFin_max lt_max Hrtt. - by rewrite /CASE_SUM.case_sumr /= 2!EFin_max 2!lt_max Hrtrue 2!orbT. - by rewrite /CASE_SUM.case_sumr /= 2!EFin_max 2!lt_max Hrfalse orbT. -move=> [x [[]|[|]]] U mU/=-. -by rewrite (Hf (inl tt) x _ mU). -by rewrite (Hf (inr true) x _ mU). -by rewrite (Hf (inr false) x _ mU). -Qed. +Reserved Notation "f .; g" (at level 60, right associativity, + format "f .; '/ ' g"). -#[export] -HB.instance Definition _ := @isSFiniteKernel_subdef.Build _ _ _ _ _ - (case_sum' k) sfinite_case_sum'. -End sfkcase_sum'. +Notation "l .; k" := (mkcomp l (mkswap k)) : ereal_scope. -End case_sum'. +Section letin'. +Variables (d d' d3 : _) (X : measurableType d) (Y : measurableType d') + (Z : measurableType d3) (R : realType). -Section case_sum. -Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). -Let A : measurableType _ := unit. -Let B : measurableType _ := bool. +Definition letin' (l : R.-sfker X ~> Y) (k : R.-sfker Y * X ~> Z) := + locked [the R.-sfker X ~> Z of l .; k]. -Import CASE_SUM. +Lemma letin'E (l : R.-sfker X ~> Y) (k : R.-sfker Y * X ~> Z) x U : + letin' l k x U = \int[l x]_y k (y, x) U. +Proof. by rewrite /letin'; unlock. Qed. -(* case analysis on the datatype unit + bool *) -Definition case_sum (f : R.-sfker X ~> (A + B)%type) - (k : (A + B)%type-> R.-sfker X ~> Y) : R.-sfker X ~> Y := - [the R.-sfker X ~> (A + B)%type of f] \; - [the R.-sfker X * (A + B) ~> Y of case_sum' k]. +Lemma letin'_letin (l : R.-sfker X ~> Y) (k : R.-sfker Y * X ~> Z) : + letin' l k = letin l (mkswap k). +Proof. by rewrite /letin'; unlock. Qed. -End case_sum. +End letin'. -(* counting measure as a kernel *) -Section kcounting. -Context d (G : measurableType d) (R : realType). +Section letin'C. +Import Notations. +Context d d1 d' (X : measurableType d) (Y : measurableType d1) + (Z : measurableType d') (R : realType). +Variables (t : R.-sfker Z ~> X) + (u' : R.-sfker X * Z ~> Y) + (u : R.-sfker Z ~> Y) + (t' : R.-sfker Y * Z ~> X) + (tt' : forall y, t =1 fun z => t' (y, z)) + (uu' : forall x, u =1 fun z => u' (x, z)). + +Definition T' z : set X -> \bar R := t z. +Let T0 z : (T' z) set0 = 0. Proof. by []. Qed. +Let T_ge0 z x : 0 <= (T' z) x. Proof. by []. Qed. +Let T_semi_sigma_additive z : semi_sigma_additive (T' z). +Proof. exact: measure_semi_sigma_additive. Qed. +HB.instance Definition _ z := @isMeasure.Build _ X R (T' z) (T0 z) (T_ge0 z) + (@T_semi_sigma_additive z). -Definition kcounting : G -> {measure set nat -> \bar R} := fun=> counting. +Let sfinT z : sfinite_measure (T' z). Proof. exact: sfinite_kernel_measure. Qed. +HB.instance Definition _ z := @isSFinite.Build _ X R (T' z) (sfinT z). -Let mkcounting U : measurable U -> measurable_fun setT (kcounting ^~ U). -Proof. by []. Qed. +Definition U' z : set Y -> \bar R := u z. +Let U0 z : (U' z) set0 = 0. Proof. by []. Qed. +Let U_ge0 z x : 0 <= (U' z) x. Proof. by []. Qed. +Let U_semi_sigma_additive z : semi_sigma_additive (U' z). +Proof. exact: measure_semi_sigma_additive. Qed. +HB.instance Definition _ z := @isMeasure.Build _ Y R (U' z) (U0 z) (U_ge0 z) + (@U_semi_sigma_additive z). -HB.instance Definition _ := isKernel.Build _ _ _ _ _ kcounting mkcounting. +Let sfinU z : sfinite_measure (U' z). Proof. exact: sfinite_kernel_measure. Qed. +HB.instance Definition _ z := @isSFinite.Build _ Y R + (U' z) (sfinU z). -Let sfkcounting : exists2 k_ : (R.-ker _ ~> _)^nat, - forall n, measure_fam_uub (k_ n) & - forall x U, measurable U -> kcounting x U = mseries (k_ ^~ x) 0 U. +Lemma letin'C z A : measurable A -> + letin' t + (letin' u' + (ret (measurable_fun_prod macc1of3' macc0of3'))) z A = + letin' u + (letin' t' + (ret (measurable_fun_prod macc0of3' macc1of3'))) z A. Proof. -exists (fun n => [the R.-fker _ ~> _ of - @kdirac _ _ G nat R _ (@measurable_cst _ _ _ _ setT n)]). - by move=> n /=; exact: measure_uub. -by move=> g U mU; rewrite /kcounting/= counting_dirac. +move=> mA. +rewrite !letin'E. +under eq_integral. + move=> x _. + rewrite letin'E -uu'. + under eq_integral do rewrite retE /=. + over. +rewrite (sfinite_Fubini (T' z) (U' z) (fun x => \d_(x.1, x.2) A ))//; last first. + apply/measurable_EFinP => /=; rewrite (_ : (fun x => _) = mindic R mA)//. + by apply/funext => -[]. +rewrite /=. +apply: eq_integral => y _. +by rewrite letin'E/= -tt'; apply: eq_integral => // x _; rewrite retE. Qed. -HB.instance Definition _ := - isSFiniteKernel_subdef.Build _ _ _ _ R kcounting sfkcounting. - -End kcounting. - -(* formalization of the iterate construct of Staton ESOP 2017, Sect. 4.2 *) -Section iterate. -Context d {G : measurableType d} {R : realType}. -Let A : measurableType _ := unit. -Let B : measurableType _ := bool. - -(* formalization of iterate^n - Gamma |-p iterate^n t from x = u : B *) -Variables (t : R.-sfker (G * A) ~> (A + B)%type) - (u : G -> A) (mu : measurable_fun setT u). +End letin'C. +Arguments letin'C {d d1 d' X Y Z R} _ _ _ _. -Fixpoint iterate_ n : R.-sfker G ~> B := - match n with - | 0%N => case_sum (letin (ret mu) t) - (fun x => match x with - | inl a => fail - | inr b => ret (measurable_cst b) - end) - | m.+1 => case_sum (letin (ret mu) t) - (fun x => match x with - | inl a => iterate_ m - | inr b => fail - end) - end. +Section letin'A. +Context d d' d1 d2 d3 (X : measurableType d) (Y : measurableType d') + (T1 : measurableType d1) (T2 : measurableType d2) (T3 : measurableType d3) + (R : realType). +Import Notations. +Variables (t : R.-sfker X ~> T1) + (u : R.-sfker T1 * X ~> T2) + (v : R.-sfker T2 * X ~> Y) + (v' : R.-sfker T2 * (T1 * X) ~> Y) + (vv' : forall y, v =1 fun xz => v' (xz.1, (y, xz.2))). -(* formalization of iterate (A = unit, B = bool) - Gamma, x : A |-p t : A + B Gamma |-d u : A ------------------------------------------------ - Gamma |-p iterate t from x = u : B *) -Definition iterate : R.-sfker G ~> B := case_nat (kcounting R) iterate_. +Lemma letin'A x A : measurable A -> + letin' t (letin' u v') x A + = + (letin' (letin' t u) v) x A. +Proof. +move=> mA. +rewrite !letin'E. +under eq_integral do rewrite letin'E. +rewrite letin'_letin/=. +rewrite integral_kcomp; [|by []|]. + apply: eq_integral => z _. + apply: eq_integral => y _. + by rewrite (vv' z). +exact: measurableT_comp (@measurable_kernel _ _ _ _ _ v _ mA) _. +Qed. -End iterate. +End letin'A. -(* an s-finite kernel to test that two expressions are different *) -Section lift_neq. -Context {R : realType} d (G : measurableType d). -Variables (f : G -> bool) (g : G -> bool). +Lemma letin'_sample_bernoulli d d' (T : measurableType d) + (T' : measurableType d') (R : realType) (r : R) (r01 : (0 <= r <= 1)%R) + (u : R.-sfker bool * T ~> T') x y : + letin' (sample_cst (bernoulli r)) u x y = + r%:E * u (true, x) y + (`1- r)%:E * u (false, x) y. +Proof. by rewrite letin'_letin letin_sample_bernoulli. Qed. -Definition flift_neq : G -> bool := fun x' => f x' != g x'. +Section letin'_return. +Context d d' d3 (X : measurableType d) (Y : measurableType d') + (Z : measurableType d3) (R : realType). -Hypotheses (mf : measurable_fun setT f) (mg : measurable_fun setT g). +Lemma letin'_kret (k : R.-sfker X ~> Y) + (f : Y * X -> Z) (mf : measurable_fun setT f) x U : + measurable U -> + letin' k (ret mf) x U = k x (curry f ^~ x @^-1` U). +Proof. +move=> mU. +rewrite letin'E. +under eq_integral do rewrite retE. +rewrite integral_indic ?setIT// -[X in measurable X]setTI. +exact: (measurableT_comp mf). +Qed. -(* see also emeasurable_fun_neq *) -Lemma measurable_fun_flift_neq : measurable_fun setT flift_neq. +Lemma letin'_retk (f : X -> Y) (mf : measurable_fun setT f) + (k : R.-sfker Y * X ~> Z) x U : + measurable U -> letin' (ret mf) k x U = k (f x, x) U. Proof. -apply: (@measurable_fun_bool _ _ _ _ true). -rewrite setTI. -rewrite /flift_neq /= (_ : _ @^-1` _ = ([set x | f x] `&` [set x | ~~ g x]) `|` - ([set x | ~~ f x] `&` [set x | g x])). - apply: measurableU; apply: measurableI. - - by rewrite -[X in measurable X]setTI; exact: mf. - - rewrite [X in measurable X](_ : _ = ~` [set x | g x]); last first. - by apply/seteqP; split => x /= /negP. - by apply: measurableC; rewrite -[X in measurable X]setTI; exact: mg. - - rewrite [X in measurable X](_ : _ = ~` [set x | f x]); last first. - by apply/seteqP; split => x /= /negP. - by apply: measurableC; rewrite -[X in measurable X]setTI; exact: mf. - - by rewrite -[X in measurable X]setTI; exact: mg. -by apply/seteqP; split => x /=; move: (f x) (g x) => [|] [|]//=; intuition. +move=> mU; rewrite letin'E retE integral_dirac ?diracT ?mul1e//. +exact: (measurableT_comp (measurable_kernel k _ mU)). Qed. -Definition lift_neq : R.-sfker G ~> bool := ret measurable_fun_flift_neq. +End letin'_return. -End lift_neq. +Section letin'_ite. +Context d d2 d3 (T : measurableType d) (T2 : measurableType d2) + (Z : measurableType d3) (R : realType). +Variables (k1 k2 : R.-sfker T ~> Z) + (u : R.-sfker Z * T ~> T2) + (f : T -> bool) (mf : measurable_fun setT f) + (t : T) (U : set T2). -Section von_neumann_trick. -Context d {T : measurableType d} {R : realType}. +Lemma letin'_iteT : f t -> letin' (ite mf k1 k2) u t U = letin' k1 u t U. +Proof. by move=> ftT; rewrite !letin'_letin letin_iteT. Qed. -Definition kinrtt {d1 d2} {T1 : measurableType d1} {T2 : measurableType d2} := - @measurable_cst _ _ T1 _ setT (@inl unit T2 tt). +Lemma letin'_iteF : ~~ f t -> letin' (ite mf k1 k2) u t U = letin' k2 u t U. +Proof. by move=> ftF; rewrite !letin'_letin letin_iteF. Qed. -Definition finlb d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) - : T1 * bool -> T2 + bool := fun t1b => inr t1b.2. +End letin'_ite. -Lemma minlb {d1 d2} {T1 : measurableType d1} {T2 : measurableType d2} : - measurable_fun setT (@finlb _ _ T1 T2). -Proof. exact: measurableT_comp. Qed. +Section hard_constraint'. +Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). -Variable (D : pprobability bool R (* biased coin *)). +Definition fail' : R.-sfker X ~> Y := + letin' (score (measurable_cst (0%R : R))) + (ret (measurable_cst point)). -Definition von_neumann_trick' : R.-sfker (T * unit) ~> (unit + bool)%type := - letin (sample_cst D) - (letin (sample_cst D) - (letin (lift_neq macc1of3 macc2of3) - (ite macc3of4 - (letin (ret macc1of4) (ret minlb)) - (ret kinrtt)))). +Lemma fail'E x U : fail' x U = 0. +Proof. by rewrite /fail' letin'_letin failE. Qed. -Definition von_neumann_trick : R.-sfker T ~> bool := - iterate von_neumann_trick' ktt. +End hard_constraint'. +Arguments fail' {d d' X Y R}. -End von_neumann_trick. +Lemma score_fail' d (X : measurableType d) {R : realType} + (r : R) (r01 : (0 <= r <= 1)%R) : + score (kr r) = + letin' (sample_cst (bernoulli r) : R.-pker X ~> _) + (ite macc0of2 (ret ktt) fail'). +Proof. +move: r01 => /andP[r0 r1]; apply/eq_sfkernel => x U. +rewrite letin'E/= /sample; unlock. +rewrite integral_bernoulli ?r0//=. +by rewrite /mscale/= iteE//= iteE//= fail'E mule0 adde0 ger0_norm. +Qed. diff --git a/theories/prob_lang_wip.v b/theories/prob_lang_wip.v index 4e03fbc74..682b13ea6 100644 --- a/theories/prob_lang_wip.v +++ b/theories/prob_lang_wip.v @@ -6,10 +6,11 @@ From mathcomp Require Import functions cardinality fsbigop. From mathcomp Require Import signed reals ereal topology normedtype sequences. From mathcomp Require Import esum measure lebesgue_measure numfun. From mathcomp Require Import lebesgue_integral exp kernel trigo prob_lang. +From mathcomp Require Import realfun charge. (******************************************************************************) (* Semantics of a probabilistic programming language using s-finite kernels *) -(* (wip about definition of Lebesgue and counting measures) *) +(* (wip about the definition of Lebesgue measure) *) (******************************************************************************) Set Implicit Arguments. @@ -22,112 +23,187 @@ Local Open Scope classical_set_scope. Local Open Scope ring_scope. Local Open Scope ereal_scope. -Section gauss. -Variable R : realType. +Section gauss_pdf. +Context {R : realType}. Local Open Scope ring_scope. -(* density function for gauss *) -Definition gauss_density m s x : R := +Definition gauss_pdf m s x : R := (s * sqrtr (pi *+ 2))^-1 * expR (- ((x - m) / s) ^+ 2 / 2%:R). -Lemma gauss_density_ge0 m s x : 0 <= s -> 0 <= gauss_density m s x. +Lemma gauss_pdf_ge0 m s x : 0 <= s -> 0 <= gauss_pdf m s x. Proof. by move=> s0; rewrite mulr_ge0 ?expR_ge0// invr_ge0 mulr_ge0. Qed. -Lemma gauss_density_gt0 m s x : 0 < s -> 0 < gauss_density m s x. +Lemma gauss_pdf_gt0 m s x : 0 < s -> 0 < gauss_pdf m s x. Proof. move=> s0; rewrite mulr_gt0 ?expR_gt0// invr_gt0 mulr_gt0//. by rewrite sqrtr_gt0 pmulrn_rgt0// pi_gt0. Qed. -Definition gauss01_density : R -> R := gauss_density 0 1. +Lemma measurable_gauss_pdf m s : measurable_fun setT (gauss_pdf m s). +Proof. +apply: measurable_funM => //=; apply: measurableT_comp => //=. +apply: measurable_funM => //=; apply: measurableT_comp => //=. +apply: measurableT_comp (exprn_measurable _) _ => /=. +by apply: measurable_funM => //=; exact: measurable_funD. +Qed. -Hypothesis integral_gauss01_density : - (\int[lebesgue_measure]_x (gauss01_density x)%:E = 1%E)%E. +Definition gauss_pdf01 : R -> R := gauss_pdf 0 1. -Lemma gauss01_densityE x : - gauss01_density x = (sqrtr (pi *+ 2))^-1 * expR (- (x ^+ 2) / 2%:R). -Proof. by rewrite /gauss01_density /gauss_density mul1r subr0 divr1. Qed. +Lemma gauss_pdf01E x : + gauss_pdf01 x = (sqrtr (pi *+ 2))^-1 * expR (- (x ^+ 2) / 2%:R). +Proof. by rewrite /gauss_pdf01 /gauss_pdf mul1r subr0 divr1. Qed. -Definition mgauss01 (V : set R) := - (\int[lebesgue_measure]_(x in V) (gauss01_density x)%:E)%E. +Lemma gauss_pdf01_ub x : gauss_pdf01 x <= (Num.sqrt (pi *+ 2))^-1. +Proof. +rewrite -[leRHS]mulr1. +rewrite /gauss_pdf01 /gauss_pdf; last first. +rewrite mul1r subr0 ler_pM2l ?invr_gt0// ?sqrtr_gt0; last by rewrite mulrn_wgt0// pi_gt0. +by rewrite -[leRHS]expR0 ler_expR mulNr oppr_le0 mulr_ge0// sqr_ge0. +Qed. -Lemma measurable_fun_gauss_density m s : - measurable_fun setT (gauss_density m s). +Lemma continuous_gauss_pdf1 x : {for x, continuous gauss_pdf01}. Proof. -apply: measurable_funM => //=. -apply: measurableT_comp => //=. -apply: measurable_funM => //=. -apply: measurableT_comp => //=. -apply: measurableT_comp (exprn_measurable _) _ => /=. -apply: measurable_funM => //=. -exact: measurable_funD. +apply: continuousM => //=; first exact: cvg_cst. +apply: continuous_comp => /=; last exact: continuous_expR. +apply: continuousM => //=; last exact: cvg_cst. +apply: continuous_comp => //=; last exact: (@continuousN _ R^o). +apply: (@continuous_comp _ _ _ _ (fun x : R => x ^+ 2)%R); last exact: exprn_continuous. +apply: continuousM => //=; last exact: cvg_cst. +by apply: (@continuousD _ R^o) => //=; exact: cvg_cst. Qed. -Let mgauss010 : mgauss01 set0 = 0%E. -Proof. by rewrite /mgauss01 integral_set0. Qed. +End gauss_pdf. + +Definition gauss01 {R : realType} + of \int[@lebesgue_measure R]_x (gauss_pdf01 x)%:E = 1%E : set _ -> \bar R := + fun V => (\int[lebesgue_measure]_(x in V) (gauss_pdf01 x)%:E)%E. + +Section gauss. +Variable R : realType. +Local Open Scope ring_scope. + +Hypothesis integral_gauss_pdf01 : + (\int[@lebesgue_measure R]_x (gauss_pdf01 x)%:E = 1%E)%E. + +Local Notation gauss01 := (gauss01 integral_gauss_pdf01). + +Let gauss010 : gauss01 set0 = 0%E. +Proof. by rewrite /gauss01 integral_set0. Qed. -Let mgauss01_ge0 A : (0 <= mgauss01 A)%E. +Let gauss01_ge0 A : (0 <= gauss01 A)%E. Proof. -by rewrite /mgauss01 integral_ge0//= => x _; rewrite lee_fin gauss_density_ge0. +by rewrite /gauss01 integral_ge0//= => x _; rewrite lee_fin gauss_pdf_ge0. Qed. -Let mgauss01_sigma_additive : semi_sigma_additive mgauss01. +Let gauss01_sigma_additive : semi_sigma_additive gauss01. Proof. move=> /= F mF tF mUF. -rewrite /mgauss01/= integral_bigcup//=; last first. +rewrite /gauss01/= integral_bigcup//=; last first. apply/integrableP; split. - apply/measurable_EFinP. - exact: measurable_funS (measurable_fun_gauss_density 0 1). - rewrite (_ : (fun x => _) = (EFin \o gauss01_density)); last first. - by apply/funext => x; rewrite gee0_abs// lee_fin gauss_density_ge0. + apply/measurable_funTS/measurableT_comp => //. + exact: measurable_gauss_pdf. + rewrite (_ : (fun x => _) = EFin \o gauss_pdf01); last first. + by apply/funext => x; rewrite gee0_abs// lee_fin gauss_pdf_ge0. apply: le_lt_trans. apply: (@ge0_subset_integral _ _ _ _ _ setT) => //=. - apply/measurable_EFinP. - exact: measurable_fun_gauss_density. - by move=> ? _; rewrite lee_fin gauss_density_ge0. - by rewrite integral_gauss01_density// ltey. + by apply/measurable_EFinP; exact: measurable_gauss_pdf. + by move=> ? _; rewrite lee_fin gauss_pdf_ge0. + by rewrite integral_gauss_pdf01 // ltey. apply: is_cvg_ereal_nneg_natsum_cond => n _ _. -by apply: integral_ge0 => /= x ?; rewrite lee_fin gauss_density_ge0. +by apply: integral_ge0 => /= x ?; rewrite lee_fin gauss_pdf_ge0. Qed. HB.instance Definition _ := isMeasure.Build _ _ _ - mgauss01 mgauss010 mgauss01_ge0 mgauss01_sigma_additive. - -Let mgauss01_setT : mgauss01 [set: _] = 1%E. -Proof. by rewrite /mgauss01 integral_gauss01_density. Qed. + gauss01 gauss010 gauss01_ge0 gauss01_sigma_additive. -HB.instance Definition _ := @Measure_isProbability.Build _ _ R mgauss01 mgauss01_setT. +Let gauss01_setT : gauss01 [set: _] = 1%E. +Proof. by rewrite /gauss01 integral_gauss_pdf01. Qed. -Definition gauss01 := [the probability _ _ of mgauss01]. +HB.instance Definition _ := @Measure_isProbability.Build _ _ R gauss01 gauss01_setT. End gauss. Section gauss_lebesgue. -Import Notations. Context d (T : measurableType d) (R : realType). -Hypothesis integral_gauss01_density : - (\int[@lebesgue_measure R]_x (gauss01_density x)%:E = 1%E)%E. +Notation mu := (@lebesgue_measure R). +Hypothesis integral_gauss_pdf01 : \int[mu]_x (gauss_pdf01 x)%:E = 1%E. + +Lemma gauss01_dom : gauss01 integral_gauss_pdf01 `<< mu. +Proof. +move=> A mA muA0; rewrite /gauss01. +apply/eqP; rewrite eq_le; apply/andP; split; last first. + by apply: integral_ge0 => x _; rewrite lee_fin gauss_pdf_ge0. +apply: (@le_trans _ _ (\int[mu]_(x in A) (Num.sqrt (pi *+ 2))^-1%:E))%E; last first. + by rewrite integral_cst//= muA0 mule0. +apply: ge0_le_integral => //=. +- by move=> x _; rewrite lee_fin gauss_pdf_ge0. +- apply/measurable_funTS/measurableT_comp => //. + exact: measurable_gauss_pdf. +- by move=> x _; rewrite lee_fin gauss_pdf01_ub. +Qed. -Let f1 (x : R) := (gauss01_density x) ^-1. +Let f1 (x : g_sigma_algebraType (R.-ocitv.-measurable)) := (gauss_pdf01 x) ^-1. -Hypothesis integral_mgauss01 : forall U, measurable U -> - \int[mgauss01 (R:=R)]_(y in U) (f1 y)%:E = - \int[lebesgue_measure]_(x0 in U) (gauss01_density x0 * f1 x0)%:E. +Lemma measurable_fun_f1 : measurable_fun setT f1. +Proof. +apply: continuous_measurable_fun => x. +apply: (@continuousV _ _ gauss_pdf01). + by rewrite gt_eqF// gauss_pdf_gt0. +exact: continuous_gauss_pdf1. +Qed. + +Lemma integrable_f1 U : measurable U -> + (gauss01 integral_gauss_pdf01).-integrable U (fun x : g_sigma_algebraType (R.-ocitv.-measurable) => (f1 x)%:E). +Proof. +Admitted. + +Lemma integral_mgauss01 : forall U, measurable U -> + \int[gauss01 integral_gauss_pdf01]_(y in U) (f1 y)%:E = + \int[mu]_(x0 in U) (gauss_pdf01 x0 * f1 x0)%:E. +Proof. +move=> U mU. +under [in RHS]eq_integral do rewrite EFinM/= muleC. +rewrite -(Radon_Nikodym_change_of_variables gauss01_dom _ (integrable_f1 mU))//=. +apply: ae_eq_integral => //=. +- apply: emeasurable_funM => //. + apply/measurable_funTS/measurableT_comp => //. + exact: measurable_fun_f1. + apply: measurable_int. + apply: integrableS (Radon_Nikodym_integrable _) => //=. + exact: gauss01_dom. +- apply: emeasurable_funM => //. + apply/measurable_funTS/measurableT_comp => //. + exact: measurable_fun_f1. + apply/measurable_funTS/measurableT_comp => //. + exact: measurable_gauss_pdf. +- apply: ae_eq_mul2l => /=. + rewrite Radon_NikodymE//=. + exact: gauss01_dom. + move=> gauss01_dom'. + case: cid => //= h [h1 h2 h3]. + apply: integral_ae_eq => //=. + + exact: integrableS h2. + + apply/measurable_funTS/measurableT_comp => //. + exact: measurable_gauss_pdf. + + by move=> E EU mE; rewrite -(h3 _ mE). +Qed. + +(*Hypothesis integral_mgauss01 : forall U, measurable U -> + \int[gauss01 integral_gauss_pdf01]_(y in U) (f1 y)%:E = + \int[mu]_(x0 in U) (gauss_pdf01 x0 * f1 x0)%:E.*) Let mf1 : measurable_fun setT f1. Proof. apply: (measurable_comp (F := [set r : R | r != 0%R])) => //. - exact: open_measurable. -- by move=> /= r [t _ <-]; rewrite gt_eqF// gauss_density_gt0. +- by move=> /= r [t _ <-]; rewrite gt_eqF// gauss_pdf_gt0. - apply: open_continuous_measurable_fun => //. by apply/in_setP => x /= x0; exact: inv_continuous. -- exact: measurable_fun_gauss_density. +- exact: measurable_gauss_pdf. Qed. -Variable mu : {measure set R -> \bar R}. - Definition staton_lebesgue : R.-sfker T ~> _ := - letin (sample_cst (gauss01 integral_gauss01_density : pprobability _ _)) + letin (sample_cst (gauss01 integral_gauss_pdf01 : pprobability _ _)) (letin (score (measurableT_comp mf1 macc1of2)) (ret macc1of3)). @@ -137,23 +213,22 @@ Lemma staton_lebesgueE x U : measurable U -> Proof. move=> mU; rewrite [in LHS]/staton_lebesgue/=. rewrite [in LHS]letinE /=. -transitivity (\int[@mgauss01 R]_(y in U) (f1 y)%:E). +transitivity (\int[gauss01 integral_gauss_pdf01]_(y in U) (f1 y)%:E). rewrite -[in RHS](setTI U) integral_mkcondr/=. apply: eq_integral => //= r _. rewrite letinE/= ge0_integral_mscale//= ger0_norm//; last first. - by rewrite invr_ge0// gauss_density_ge0. + by rewrite invr_ge0// gauss_pdf_ge0. rewrite integral_dirac// diracT mul1e/= diracE epatch_indic/=. by rewrite indicE. rewrite integral_mgauss01//. transitivity (\int[lebesgue_measure]_(x in U) (\1_U x)%:E). apply: eq_integral => /= y yU. - by rewrite /f1 divrr ?indicE ?yU// unitfE gt_eqF// gauss_density_gt0. + by rewrite /f1 divrr ?indicE ?yU// unitfE gt_eqF// gauss_pdf_gt0. by rewrite integral_indic//= setIid. Qed. End gauss_lebesgue. -(* TODO: move this elsewhere *) (* assuming x > 0 *) Definition Gamma {R : realType} (x : R) : \bar R := \int[lebesgue_measure]_(t in `[0%R, +oo[%classic) (expR (- t) * powR t (x - 1))%:E. @@ -163,20 +238,21 @@ Definition Rfact {R : realType} (x : R) := Gamma (x + 1)%R. Section poisson. Variable R : realType. Local Open Scope ring_scope. +Notation mu := (@lebesgue_measure R). Hypothesis integral_poisson_density : forall k, - (\int[lebesgue_measure]_x (@poisson R k x)%:E = 1%E)%E. + (\int[mu]_x (@poisson_pdf R k x)%:E = 1%E)%E. (* density function for poisson *) -Definition poisson1 := @poisson R 1%N. +Definition poisson1 := @poisson_pdf R 1%N. Lemma poisson1_ge0 (x : R) : 0 <= poisson1 x. -Proof. exact: poisson_ge0. Qed. +Proof. exact: poisson_pdf_ge0. Qed. Definition mpoisson1 (V : set R) : \bar R := (\int[lebesgue_measure]_(x in V) (poisson1 x)%:E)%E. Lemma measurable_fun_poisson1 : measurable_fun setT poisson1. -Proof. exact: measurable_poisson. Qed. +Proof. exact: measurable_poisson_pdf. Qed. Let mpoisson10 : mpoisson1 set0 = 0%E. Proof. by rewrite /mpoisson1 integral_set0. Qed. @@ -215,7 +291,8 @@ rewrite /poisson1. by rewrite integral_poisson_density. Qed. -HB.instance Definition _ := @Measure_isProbability.Build _ _ R mpoisson1 mpoisson1_setT. +HB.instance Definition _ := @Measure_isProbability.Build _ _ R + mpoisson1 mpoisson1_setT. Definition poisson' := [the probability _ _ of mpoisson1]. @@ -226,21 +303,22 @@ End poisson. Section staton_counting. Context d (X : measurableType d). Variable R : realType. +Notation mu := (@lebesgue_measure R). Import Notations. Hypothesis integral_poisson_density : forall k, - (\int[lebesgue_measure]_x (@poisson R k x)%:E = 1%E)%E. + (\int[mu]_x (@poisson_pdf R k x)%:E = 1%E)%E. Let f1 x := (poisson1 (x : R)) ^-1. Let mf1 : measurable_fun setT f1. -rewrite /f1 /poisson1 /poisson. +rewrite /f1 /poisson1 /poisson_pdf. apply: (measurable_comp (F := [set r : R | r != 0%R])) => //. - exact: open_measurable. - move=> /= r [t ? <-]. by case: ifPn => // t0; rewrite gt_eqF ?mulr_gt0 ?expR_gt0//= invrK ltr0n. - apply: open_continuous_measurable_fun => //. by apply/in_setP => x /= x0; exact: inv_continuous. -- exact: measurable_poisson. +- exact: measurable_poisson_pdf. Qed. Definition staton_counting : R.-sfker X ~> _ := From 898b9e094e209bc53063e7e60bbedd524a5aa1e7 Mon Sep 17 00:00:00 2001 From: IshiguroYoshihiro Date: Sun, 25 Aug 2024 16:29:03 +0900 Subject: [PATCH 27/36] beta_bdf_uniq_ae, integral_indicator_function --- theories/lang_syntax.v | 180 +++++++++++++++++++++++++++--- theories/lang_syntax_examples.v | 6 +- theories/lang_syntax_table_game.v | 8 +- 3 files changed, 170 insertions(+), 24 deletions(-) diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index 5a1af280e..73e61630e 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -74,6 +74,130 @@ Local Open Scope classical_set_scope. Local Open Scope ring_scope. Local Open Scope ereal_scope. +(* In this module, we use our lemma continuous_FTC2 to compute the value of + * integration of the indicator function over the interval [0, 1]. + * we can use our lemma continuous_FTC2 because it requires continuous + * within [0, 1], which the indicator function satisfies. + * we also shows that the indicator function is not continuous in [0, 1], + * required by previous version of lemma continuous_FTC2. This shows that + * our lemma continuous_FTC2 is + * enough weak to be usable in practice. + *) +Module integral_indicator_function. +Section integral_indicator_function. + +Context {R : realType}. +Notation mu := lebesgue_measure. +Local Open Scope ereal_scope. +Implicit Types (f : R -> R) (a b : R). + +Local Import set_interval. + +Let uni := @indic R R `[0%R, 1%R]%classic. + +Let integrable_uni : mu.-integrable setT (EFin \o uni). +Proof. +apply/integrableP; split. + apply: measurableT_comp => //. + exact: measurable_indic. +apply/abse_integralP => //. + apply: measurableT_comp => //. + exact: measurable_indic. +rewrite -ge0_fin_numE; last exact: abse_ge0. +rewrite abse_fin_num integral_indic// setIT. +by rewrite /= lebesgue_measure_itv ifT. +Qed. + +Let cuni_within : {within `[0%R, 1%R], continuous uni}. +Proof. +apply/continuous_within_itvP => //; split => //. +- move=> x x01. + apply: (@near_cst_continuous R R 1%R). + near=> z. + rewrite /uni indic_restrict patchE ifT//. + rewrite inE/=. + apply: subset_itv_oo_cc. + near: z. + exact: near_in_itvoo. +- rewrite (_: uni 0 = 1%R); last first. + rewrite /uni indic_restrict patchE ifT//. + by rewrite inE/= boundl_in_itv bnd_simp/=. + apply: cvg_near_cst. + near=> z. + rewrite /uni indic_restrict patchE ifT// inE/= in_itv/=; apply/andP; split => //. + near: z. + exact: nbhs_right_le. +- rewrite (_:uni 1 = 1%R); last first. + rewrite /uni indic_restrict patchE ifT//. + by rewrite inE/= boundr_in_itv bnd_simp/=. + apply: cvg_near_cst. + near=> z. + rewrite /uni indic_restrict patchE ifT// inE/= in_itv/=; apply/andP; split => //. + near: z. + exact: nbhs_left_ge. +Unshelve. all: end_near. Qed. + +Example cuni : ~ {in `[0%R, 1%R], continuous uni}. +Proof. +rewrite -existsNE/=. +exists 0%R. +rewrite not_implyE; split; first by rewrite boundl_in_itv/= bnd_simp. +move/left_right_continuousP. +apply/not_andP; left. +move/(@cvgrPdist_le _ R^o). +apply/existsNP. +exists (2%:R^-1). +rewrite not_implyE; split; first by rewrite invr_gt0. +move=> [e /= e0]. +move/(_ (-(e / 2))%R). +apply/not_implyP; split. + rewrite /= sub0r opprK ger0_norm; last by rewrite divr_ge0// ltW. + rewrite -{1}(add0r e). + exact: (midf_lt e0).2. +apply/not_implyP; split. + rewrite oppr_lt0. + exact: divr_gt0. +apply/negP; rewrite -ltNge. +rewrite /uni !indic_restrict !patchE. +rewrite ifT; last by rewrite inE/= boundl_in_itv/= bnd_simp. +rewrite ifF; last first. + apply: negbTE; apply/negP. + rewrite inE/= in_itv/=. + apply/negP; rewrite negb_and; apply/orP; left. + by rewrite -ltNge oppr_lt0 divr_gt0. +rewrite /point/= {2}/1%R/= subr0. +rewrite ger0_norm//. +rewrite invf_lt1//. +rewrite {1}(_:1%R = 1%:R)//; apply: ltr_nat. +Qed. + +Let dintuni : derivable_oo_continuous_bnd (@id R^o) 0 1. +Proof. +split. +- move=> x _. + exact: derivable_id. +- exact: cvg_at_right_filter. +- exact: cvg_at_left_filter. +Qed. + +Let intuni'uni : {in `]0%R, 1%R[, (@id R^o)^`() =1 uni}. +Proof. +move=> x x01. +rewrite derive1E derive_id. +rewrite /uni indic_restrict patchE ifT// inE/=. +exact: subset_itv_oo_cc. +Qed. + +Lemma intuni1 : (\int[mu]_(x in `[0, 1]) uni x)%R = 1%R. +Proof. +rewrite [RHS](_:1%R = fine (1%:E))//; congr (fine _). +rewrite (continuous_FTC2 ltr01 cuni_within dintuni intuni'uni). +by rewrite sube0. +Qed. + +End integral_indicator_function. +End integral_indicator_function. + Lemma RintegralZl d {T : measurableType d} {R : realType} {mu : measure T R} {D : set T} : d.-measurable D -> forall f : T -> R, @@ -253,13 +377,11 @@ Qed. Lemma Ronem_change (G : R -> R) (r : R) : (0 < r <= 1)%R -> - locally_integrable [set: R] G -> {within `[0%R, r], continuous G} -> - (forall r, lebesgue_measure.-integrable `[0%R, r] (EFin \o G)) -> (\int[mu]_(x in `[0%R, r]) (G x) = \int[mu]_(x in `[(1 - r)%R, 1%R]) (G (1 - x)))%R. Proof. -move=> r01 locG cG iG. +move=> r01 cG. rewrite [in LHS]/Rintegral. by rewrite onem_change. Qed. @@ -707,19 +829,10 @@ Qed. Lemma betafun_sym (a b : nat) : betafun a b = betafun b a :> R. Proof. rewrite -[LHS]Rintegral_mkcond. -rewrite Ronem_change//=; last 4 first. +rewrite Ronem_change//=; last 2 first. by rewrite ltr01 lexx. - split. - - exact: measurable_fun_XMonemX. - - exact: openT. - - move=> K _ cK. - by have /integrableP[] := @continuous_compact_integrable R _ K cK - (@within_continuous_XMonemX R a.-1 b.-1 K). apply: continuous_subspaceT. by move=> x x01; exact: continuous_XMonemX. - move=> r. - exact: (@continuous_compact_integrable R _ _ - (@segment_compact _ _ _) (@within_continuous_XMonemX R a.-1 b.-1 `[0%R, r])). rewrite subrr. rewrite -[RHS]Rintegral_mkcond. apply: eq_Rintegral => x x01. @@ -879,7 +992,7 @@ Proof. by rewrite -[LHS]fineK ?betafunEFin_fin_num. Qed. Lemma integrable_XMonemX01 a b : mu.-integrable setT (EFin \o XMonemX01 a b). Proof. apply/integrableP; split. - by apply/EFin_measurable_fun; exact: measurable_XMonemX01. + by apply/measurable_EFinP; exact: measurable_XMonemX01. under eq_integral. move=> /= x _. rewrite ger0_norm//; last by rewrite XMonemX01_ge0. @@ -1212,7 +1325,39 @@ apply: ge0_le_integral => //=; first exact: measurableI. - by move=> x _; rewrite lee_fin XMonemX01_le1. Qed. -Lemma integral_beta_prob a b f U : measurable U -> measurable_fun U f -> +Section beta_pdf_Beta. + +Local Open Scope charge_scope. + +(* beta_pdf is almost density function of Beta *) +Lemma beta_pdf_uniq_ae (a b : nat) : + ae_eq mu `[0%R, 1%R]%classic + ('d ((charge_of_finite_measure (@beta_prob R a b))) '/d mu) + (EFin \o (beta_pdf a b)). +Proof. +apply: integral_ae_eq => //. +- apply: integrableS (Radon_Nikodym_integrable _) => //. + exact: beta_prob_dom. +- apply/measurable_funTS/measurableT_comp => //. + exact: measurable_beta_pdf. +- move=> E E01 mE. + rewrite integral_beta_pdf//. + apply/esym. + rewrite -Radon_Nikodym_integral//=. + exact: beta_prob_dom. +Qed. + +(* need to add lemma about radon-nikodym derivative of + lebesgue_stieltjes measure w.r.t. continuous density function *) +(*Lemma beta_pdf_uniq (a b : nat) : + {in `[0%R, 1%R]%classic, + ('d ((charge_of_finite_measure (@Beta R a b))) '/d mu) =1 + (EFin \o (beta_pdf a b))}. +Proof. Abort.*) + +End beta_pdf_Beta. + +Lemma integral_Beta a b f U : measurable U -> measurable_fun U f -> \int[beta_prob a b]_(x in U) `|f x| < +oo -> \int[beta_prob a b]_(x in U) f x = \int[mu]_(x in U) (f x * (beta_pdf a b x)%:E) :> \bar R. Proof. @@ -1229,6 +1374,7 @@ apply: ae_eq_integral => //. rewrite Radon_NikodymE//=; first exact: beta_prob_dom. move=> ?. case: cid => /= h [h1 h2 h3]. +(* uniqueness of Radon-Nikodym derivertive up to equal on non null sets of mu *) apply: integral_ae_eq => //. + apply: integrableS h2 => //. (* integrableST? *) apply/measurable_funTS/measurableT_comp => //. @@ -1302,7 +1448,7 @@ rewrite integralD//=; last 2 first. congr (_ + _). rewrite integralZr//=; last exact: beta_prob_integrable. congr (_ * _)%E. - rewrite integral_beta_prob//; last 2 first. + rewrite integral_Beta//; last 2 first. by apply/measurableT_comp => //; exact: measurable_fun_XMonemX. by have /integrableP[_] := @beta_prob_integrable R a b c d. rewrite /beta_pdf. @@ -1329,7 +1475,7 @@ under eq_integral do rewrite muleC. rewrite /=. rewrite integralZl//=; last exact: beta_prob_integrable_onem. rewrite muleC; congr (_ * _)%E. -rewrite integral_beta_prob//=; last 2 first. +rewrite integral_Beta//=; last 2 first. apply/measurableT_comp => //=. by apply/measurable_funB => //; exact: measurable_fun_XMonemX. by have /integrableP[] := @beta_prob_integrable_onem R a b c d. diff --git a/theories/lang_syntax_examples.v b/theories/lang_syntax_examples.v index 95920323e..666136fd2 100644 --- a/theories/lang_syntax_examples.v +++ b/theories/lang_syntax_examples.v @@ -611,12 +611,12 @@ rewrite execP_letin !execP_sample execD_beta !execD_bernoulli/=. rewrite !execD_real/= exp_var'E (execD_var_erefl "p")/=. transitivity (beta_prob_bernoulli 6 4 1 0 U : \bar R). rewrite /beta_prob_bernoulli !letin'E/=. - rewrite integral_beta_prob//=; last 2 first. + rewrite integral_Beta//=; last 2 first. exact: measurable_bernoulli2. exact: integral_beta_prob_bernoulli_lty. - rewrite integral_beta_prob//=; last 2 first. + rewrite integral_Beta//=; last 2 first. by apply: measurable_funTS => /=; exact: measurable_bernoulli_XMonemX01. - rewrite integral_beta_prob//=. + rewrite integral_Beta//=. + suff: mu.-integrable `[0%R, 1%R] (fun x => bernoulli (XMonemX01 2 1 x) U * (beta_pdf 6 4 x)%:E)%E. move=> /integrableP[_]. diff --git a/theories/lang_syntax_table_game.v b/theories/lang_syntax_table_game.v index f25dac710..d3477cd2c 100644 --- a/theories/lang_syntax_table_game.v +++ b/theories/lang_syntax_table_game.v @@ -485,14 +485,14 @@ under [in RHS]eq_integral => y _. rewrite /=. rewrite [RHS]ge0_integral_mscale//=; last first. by move=> _ _; rewrite integral_ge0. -rewrite integral_beta_prob//=; last 2 first. +rewrite integral_Beta//=; last 2 first. - apply: emeasurable_funM => //=. exact: measurable_bernoulli_onemXn. apply/measurable_EFinP; apply: measurableT_comp => //. by apply: measurable_funM => //; exact: measurable_fun_XMonemX. - by have /integrableP[] := @integrable_bernoulli_XMonemX R U. rewrite ger0_norm// integral_dirac// diracT mul1e. -rewrite integral_beta_prob/=; [|by []|exact: measurable_bernoulli_onemXn +rewrite integral_Beta/=; [|by []|exact: measurable_bernoulli_onemXn |exact: integral_beta_prob_bernoulli_onem_lty]. rewrite -integralZl//=; last exact: integrable_bernoulli_beta_pdf. apply: eq_integral => y _. @@ -518,11 +518,11 @@ Lemma int_beta_prob01 {R : realType} (f : R -> R) a b U : \int[beta_prob a b]_(y in `[0%R, 1%R] : set R) bernoulli (f y) U. Proof. move=> mf f01. -rewrite [LHS]integral_beta_prob//=; last 2 first. +rewrite [LHS]integral_Beta//=; last 2 first. apply: measurable_funTS. by apply: (measurableT_comp (measurable_bernoulli2 _)) => //=. exact: integral_beta_prob_bernoulli_lty. -rewrite [RHS]integral_beta_prob//; last 2 first. +rewrite [RHS]integral_Beta//; last 2 first. apply/measurable_funTS => //=. by apply: (measurableT_comp (measurable_bernoulli2 _)) => //=. apply: (le_lt_trans _ (lang_syntax.integral_beta_prob_bernoulli_lty a b U mf f01)). From 7c5bbc0dde1dd017d0680bfe5ea7d3b74c015bfb Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Wed, 18 Sep 2024 15:18:29 +0900 Subject: [PATCH 28/36] fix --- theories/ftc.v | 39 ------------- theories/lang_syntax.v | 2 +- theories/lebesgue_integral.v | 12 ---- theories/prob_lang.v | 108 ----------------------------------- 4 files changed, 1 insertion(+), 160 deletions(-) diff --git a/theories/ftc.v b/theories/ftc.v index 4e2cab72c..08e876b95 100644 --- a/theories/ftc.v +++ b/theories/ftc.v @@ -29,45 +29,6 @@ Import numFieldNormedType.Exports. Local Open Scope classical_set_scope. Local Open Scope ring_scope. -(* TODO: move to normedtype.v? *) -Lemma nbhs_right_lt_lt {R : realType} (x y : R) : - (y < x)%R -> \forall z \near nbhs y^'+, (z < x)%R. -Proof. -move=> yx. -exists (x - y)%R => /=; first by rewrite subr_gt0. -move=> z/= /[swap] yz. -by rewrite ltr0_norm ?subr_lt0// opprB ltrBlDl addrC subrK. -Qed. - -(* TODO: move to normedtype.v? *) -Lemma nbhs_right_lt_le {R : realType} (x y : R) : - (y < x)%R -> \forall z \near nbhs y^'+, (z <= x)%R. -Proof. -by move=> yx; near=> z; apply/ltW; near: z; exact: nbhs_right_lt_lt. -Unshelve. all: by end_near. Qed. - -(* TODO: move to normedtype.v? *) -Lemma cvg_patch {R : realType} (f : R -> R^o) (a b : R) (x : R) : (a < b)%R -> - x \in `]a, b[ -> - f @ (x : subspace `[a, b]) --> f x -> - (f \_ `[a, b] x) @[x --> x] --> f x. -Proof. -move=> ab xab xf; apply/cvgrPdist_lt => /= e e0. -move/cvgrPdist_lt : xf => /(_ e e0) xf. -rewrite near_simpl; near=> z. -rewrite patchE ifT//; last first. - rewrite inE; apply: subset_itv_oo_cc. - by near: z; exact: near_in_itv. -near: z. -rewrite /prop_near1 /nbhs/= /nbhs_subspace ifT// in xf; last first. - by rewrite inE/=; exact: subset_itv_oo_cc xab. -case: xf => x0 /= x00 xf. -near=> z. -apply: xf => //=. -rewrite inE; apply: subset_itv_oo_cc. -by near: z; exact: near_in_itv. -Unshelve. all: by end_near. Qed. - Section FTC. Context {R : realType}. Notation mu := (@lebesgue_measure R). diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index 73e61630e..d0be97a7a 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -110,7 +110,7 @@ Qed. Let cuni_within : {within `[0%R, 1%R], continuous uni}. Proof. -apply/continuous_within_itvP => //; split => //. +apply/continuous_within_itvP => //; split. - move=> x x01. apply: (@near_cst_continuous R R 1%R). near=> z. diff --git a/theories/lebesgue_integral.v b/theories/lebesgue_integral.v index 5ebcac3f8..284f18536 100644 --- a/theories/lebesgue_integral.v +++ b/theories/lebesgue_integral.v @@ -7196,18 +7196,6 @@ Qed. End nicely_shrinking. -Section set_itv_porderType. -Variables (d : Order.disp_t) (T : porderType d). -Implicit Types (x y : T). - -Lemma subset_itv' x y z u : (x < y)%O -> (z < u)%O -> `[y, z] `<=` `]x, u[. -Proof. -move=> xy zu w/=; rewrite !in_itv/= => /andP[yw wz]. -by rewrite (lt_le_trans xy)//= (le_lt_trans wz). -Qed. - -End set_itv_porderType. - Section nice_lebesgue_differentiation. Local Open Scope ereal_scope. Context {R : realType}. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index d6c2c03e6..b4352847c 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -80,35 +80,6 @@ Definition dep_uncurry (A : Type) (B : A -> Type) (C : Type) : (forall a : A, B a -> C) -> {a : A & B a} -> C := fun f p => let (a, Ba) := p in f a Ba. -Section binomial_total. -Local Open Scope ring_scope. -Variables (R : realType) (n : nat). -Implicit Type p : R. - -Lemma measurable_binomial_prob : - measurable_fun setT (binomial_prob n : R -> pprobability _ _). -Proof. -apply: (@measurability _ _ _ _ _ _ - (@pset _ _ _ : set (set (pprobability _ R)))) => //. -move=> _ -[_ [r r01] [Ys mYs <-]] <-; apply: emeasurable_fun_infty_o => //=. -rewrite /binomial_prob/=. -set f := (X in measurable_fun _ X). -apply: measurable_fun_if => //=. - by apply: measurable_and => //; exact: measurable_fun_ler. -apply: (eq_measurable_fun (fun t => - \sum_(k x /set_mem[_/= x01]. - rewrite nneseries_esum// -1?[in RHS](set_mem_set Ys)// => k kYs. - by rewrite lee_fin binomial_pmf_ge0. -apply: ge0_emeasurable_sum. - by move=> k x/= [_ x01] _; rewrite lee_fin binomial_pmf_ge0. -move=> k Ysk; apply/measurableT_comp => //. -exact: measurable_binomial_pmf. -Qed. - -End binomial_total. -Arguments measurable_binomial_prob {R}. - Section poisson_pdf. Variable R : realType. Local Open Scope ring_scope. @@ -253,85 +224,6 @@ Arguments p14 {R}. (*Arguments p27 {R}.*) Arguments p1S {R}. -Section uniform_probability. -Context (R : realType) (a b : R) (ab0 : (0 < b - a)%R). - -Definition uniform_probability (* : set _ -> \bar R *) := - @mscale _ _ R (invr_nonneg (NngNum (ltW ab0))) - (mrestr lebesgue_measure (measurable_itv `[a, b])). - -(* NB: set R -> \bar R を書くとMeasure.onが通らない *) -HB.instance Definition _ := Measure.on uniform_probability. - -(* Let uniform0 : uniform_probability set0 = 0. -Proof. exact: measure0. Qed. - -Let uniform_ge0 U : 0 <= uniform_probability U. -Proof. exact: measure_ge0. Qed. - -Let uniform_sigma_additive : semi_sigma_additive uniform_probability. -Proof. move=> /= F mF tF mUF; exact: measure_semi_sigma_additive. Qed. - -HB.instance Definition _ := isMeasure.Build _ _ _ uniform_probability - uniform0 uniform_ge0 uniform_sigma_additive. *) - -Let uniform_probability_setT : uniform_probability [set: _] = 1%:E. -Proof. -rewrite /uniform_probability /mscale/= /mrestr/=. -rewrite setTI lebesgue_measure_itv/= lte_fin. -by rewrite -subr_gt0 ab0 -EFinD -EFinM mulVf// gt_eqF// subr_gt0. -Qed. - -HB.instance Definition _ := @Measure_isProbability.Build _ _ R - uniform_probability uniform_probability_setT. - -End uniform_probability. - -Section integral_uniform. -Context {R : realType}. - -Let integral_uniform_indic (a b : R) (ab0 : (0 < b - a)%R) E : - measurable E -> let m := uniform_probability ab0 in - \int[m]_x (\1_E x)%:E = - (b - a)^-1%:E * \int[lebesgue_measure]_(x in `[a, b]) (\1_E x)%:E. -Proof. -move=> mE m. -by rewrite !integral_indic//= /uniform_probability/= /mscale/= /mrestr setIT. -Qed. - -Import HBNNSimple. - -Let integral_uniform_nnsfun (f : {nnsfun _ >-> R}) - (a b : R) (ab0 : (0 < b - a)%R) : let m := uniform_probability ab0 in - \int[m]_x (f x)%:E = - (b - a)^-1%:E * \int[lebesgue_measure]_(x in `[a, b]) (f x)%:E. -Proof. -move=> m. -under [LHS]eq_integral do rewrite fimfunE -fsumEFin//. -rewrite [LHS]ge0_integral_fsum//; last 2 first. - - by move=> r; exact/measurable_EFinP/measurableT_comp. - - by move=> n x _; rewrite EFinM nnfun_muleindic_ge0. -rewrite -[RHS]ge0_integralZl//; last 3 first. - - exact/measurable_EFinP/measurable_funTS. - - by move=> x _; rewrite lee_fin. - - by rewrite lee_fin invr_ge0// ltW. -under [RHS]eq_integral. - move=> x xD; rewrite fimfunE -fsumEFin// ge0_mule_fsumr; last first. - by move=> r; rewrite EFinM nnfun_muleindic_ge0. - over. -rewrite [RHS]ge0_integral_fsum//; last 2 first. - - by move=> r; apply/measurable_EFinP; do 2 apply/measurableT_comp => //. - - move=> n x _; rewrite EFinM mule_ge0//; last by rewrite nnfun_muleindic_ge0. - by rewrite lee_fin invr_ge0// ltW. -apply: eq_fsbigr => r _; rewrite ge0_integralZl//. -- by rewrite !integralZl_indic_nnsfun//= integral_uniform_indic// muleCA. -- exact/measurable_EFinP/measurableT_comp. -- by move=> t _; rewrite nnfun_muleindic_ge0. -- by rewrite lee_fin invr_ge0// ltW. -Qed. - -End integral_uniform. - Section mscore. Context d (T : measurableType d) (R : realType). Variable f : T -> R. From 273707f9a087e8c7d9a352a9fc1ff9326afd149b Mon Sep 17 00:00:00 2001 From: Cyril Cohen Date: Thu, 10 Oct 2024 22:35:14 +0900 Subject: [PATCH 29/36] add dep to algebra-tactics in nix --- .github/workflows/nix-action-8.19.yml | 8 ++ .github/workflows/nix-action-8.20.yml | 8 ++ .github/workflows/nix-action-master.yml | 1 + .github/workflows/nix-action.yml | 46 +++++++++ .../mathcomp-analysis/default.nix | 93 +++++++++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 .github/workflows/nix-action.yml create mode 100644 .nix/coq-overlays/mathcomp-analysis/default.nix diff --git a/.github/workflows/nix-action-8.19.yml b/.github/workflows/nix-action-8.19.yml index 27378388f..aa6085628 100644 --- a/.github/workflows/nix-action-8.19.yml +++ b/.github/workflows/nix-action-8.19.yml @@ -191,6 +191,10 @@ jobs: name: 'Building/fetching previous CI target: mathcomp-bigenough' run: NIXPKGS_ALLOW_UNFREE=1 nix-build --no-out-link --argstr bundle "8.19" --argstr job "mathcomp-bigenough" + - if: steps.stepCheck.outputs.status == 'built' + name: 'Building/fetching previous CI target: mathcomp-algebra-tactics' + run: NIXPKGS_ALLOW_UNFREE=1 nix-build --no-out-link --argstr bundle "8.19" --argstr + job "mathcomp-algebra-tactics" - if: steps.stepCheck.outputs.status == 'built' name: 'Building/fetching previous CI target: hierarchy-builder' run: NIXPKGS_ALLOW_UNFREE=1 nix-build --no-out-link --argstr bundle "8.19" --argstr @@ -267,6 +271,10 @@ jobs: name: 'Building/fetching previous CI target: mathcomp-bigenough' run: NIXPKGS_ALLOW_UNFREE=1 nix-build --no-out-link --argstr bundle "8.19" --argstr job "mathcomp-bigenough" + - if: steps.stepCheck.outputs.status == 'built' + name: 'Building/fetching previous CI target: mathcomp-algebra-tactics' + run: NIXPKGS_ALLOW_UNFREE=1 nix-build --no-out-link --argstr bundle "8.19" --argstr + job "mathcomp-algebra-tactics" - if: steps.stepCheck.outputs.status == 'built' name: 'Building/fetching previous CI target: hierarchy-builder' run: NIXPKGS_ALLOW_UNFREE=1 nix-build --no-out-link --argstr bundle "8.19" --argstr diff --git a/.github/workflows/nix-action-8.20.yml b/.github/workflows/nix-action-8.20.yml index c3f3dd587..5c34e86d4 100644 --- a/.github/workflows/nix-action-8.20.yml +++ b/.github/workflows/nix-action-8.20.yml @@ -191,6 +191,10 @@ jobs: name: 'Building/fetching previous CI target: mathcomp-bigenough' run: NIXPKGS_ALLOW_UNFREE=1 nix-build --no-out-link --argstr bundle "8.20" --argstr job "mathcomp-bigenough" + - if: steps.stepCheck.outputs.status == 'built' + name: 'Building/fetching previous CI target: mathcomp-algebra-tactics' + run: NIXPKGS_ALLOW_UNFREE=1 nix-build --no-out-link --argstr bundle "8.20" --argstr + job "mathcomp-algebra-tactics" - if: steps.stepCheck.outputs.status == 'built' name: 'Building/fetching previous CI target: hierarchy-builder' run: NIXPKGS_ALLOW_UNFREE=1 nix-build --no-out-link --argstr bundle "8.20" --argstr @@ -267,6 +271,10 @@ jobs: name: 'Building/fetching previous CI target: mathcomp-bigenough' run: NIXPKGS_ALLOW_UNFREE=1 nix-build --no-out-link --argstr bundle "8.20" --argstr job "mathcomp-bigenough" + - if: steps.stepCheck.outputs.status == 'built' + name: 'Building/fetching previous CI target: mathcomp-algebra-tactics' + run: NIXPKGS_ALLOW_UNFREE=1 nix-build --no-out-link --argstr bundle "8.20" --argstr + job "mathcomp-algebra-tactics" - if: steps.stepCheck.outputs.status == 'built' name: 'Building/fetching previous CI target: hierarchy-builder' run: NIXPKGS_ALLOW_UNFREE=1 nix-build --no-out-link --argstr bundle "8.20" --argstr diff --git a/.github/workflows/nix-action-master.yml b/.github/workflows/nix-action-master.yml index 81711ae1c..d79280162 100644 --- a/.github/workflows/nix-action-master.yml +++ b/.github/workflows/nix-action-master.yml @@ -321,6 +321,7 @@ jobs: needs: - coq - mathcomp-finmap + - hierarchy-builder - mathcomp-bigenough - mathcomp-bigenough - hierarchy-builder diff --git a/.github/workflows/nix-action.yml b/.github/workflows/nix-action.yml new file mode 100644 index 000000000..06697578d --- /dev/null +++ b/.github/workflows/nix-action.yml @@ -0,0 +1,46 @@ +# This file was generated from `meta.yml`, please do not edit manually. +# Follow the instructions on https://github.com/coq-community/templates to regenerate. +name: Nix CI + +on: + push: + branches: + - master + pull_request: + paths: + - .github/workflows/** + pull_request_target: + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + overrides: + - 'coq = "master"' + fail-fast: false + steps: + - name: Determine which commit to test + run: | + if [[ ${{ github.event_name }} =~ "pull_request" ]]; then + merge_commit=$(git ls-remote ${{ github.event.repository.html_url }} refs/pull/${{ github.event.number }}/merge | cut -f1) + if [ -z "$merge_commit" ]; then + echo "tested_commit=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV + else + echo "tested_commit=$merge_commit" >> $GITHUB_ENV + fi + else + echo "tested_commit=${{ github.sha }}" >> $GITHUB_ENV + fi + - uses: cachix/install-nix-action@v14 + with: + nix_path: nixpkgs=channel:nixpkgs-unstable + - uses: cachix/cachix-action@v10 + with: + name: coq-community + extraPullNames: coq, math-comp + - uses: actions/checkout@v2 + with: + ref: ${{ env.tested_ref }} + - run: > + nix-build https://coq.inria.fr/nix/toolbox --argstr job analysis --arg override '{ ${{ matrix.overrides }}; analysis = builtins.filterSource (path: _: baseNameOf path != ".git") ./.; }' diff --git a/.nix/coq-overlays/mathcomp-analysis/default.nix b/.nix/coq-overlays/mathcomp-analysis/default.nix new file mode 100644 index 000000000..4e527a820 --- /dev/null +++ b/.nix/coq-overlays/mathcomp-analysis/default.nix @@ -0,0 +1,93 @@ +{ lib, + mkCoqDerivation, recurseIntoAttrs, + mathcomp, mathcomp-finmap, mathcomp-bigenough, + hierarchy-builder, mathcomp-algebra-tactics, + single ? false, + coqPackages, coq, version ? null }@args: +with builtins // lib; +let + repo = "analysis"; + owner = "math-comp"; + + release."0.6.1".sha256 = "sha256-1VyNXu11/pDMuH4DmFYSUF/qZ4Bo+/Zl3Y0JkyrH/r0="; + release."0.6.0".sha256 = "sha256-0msICcIrK6jbOSiBu0gIVU3RHwoEEvB88CMQqW/06rg="; + release."0.5.3".sha256 = "sha256-1NjFsi5TITF8ZWx1NyppRmi8g6YaoUtTdS9bU/sUe5k="; + release."0.5.2".sha256 = "0yx5p9zyl8jv1vg7rgkyq8dqzkdnkqv969mi62whmhkvxbavgzbw"; + release."0.5.1".sha256 = "1hnzqb1gxf88wgj2n1b0f2xm6sxg9j0735zdsv6j12hlvx5lwk68"; + release."0.3.13".sha256 = "sha256-Yaztew79KWRC933kGFOAUIIoqukaZOdNOdw4XszR1Hg="; + release."0.3.10".sha256 = "sha256-FBH2c8QRibq5Ycw/ieB8mZl0fDiPrYdIzZ6W/A3pIhI="; + release."0.3.9".sha256 = "sha256-uUU9diBwUqBrNRLiDc0kz0CGkwTZCUmigPwLbpDOeg4="; + release."0.3.6".sha256 = "0g2j7b2hca4byz62ssgg90bkbc8wwp7xkb2d3225bbvihi92b4c5"; + release."0.3.4".sha256 = "18mgycjgg829dbr7ps77z6lcj03h3dchjbj5iir0pybxby7gd45c"; + release."0.3.3".sha256 = "1m2mxcngj368vbdb8mlr91hsygl430spl7lgyn9qmn3jykack867"; + release."0.3.1".sha256 = "1iad288yvrjv8ahl9v18vfblgqb1l5z6ax644w49w9hwxs93f2k8"; + release."0.2.3".sha256 = "0p9mr8g1qma6h10qf7014dv98ln90dfkwn76ynagpww7qap8s966"; + + defaultVersion = with versions; lib.switch [ coq.version mathcomp.version ] [ + { cases = [ (isGe "8.14") (isGe "1.13.0") ]; out = "0.6.1"; } + { cases = [ (isGe "8.14") (range "1.13" "1.15") ]; out = "0.5.2"; } + { cases = [ (range "8.13" "8.15") (range "1.13" "1.14") ]; out = "0.5.1"; } + { cases = [ (range "8.13" "8.15") (range "1.12" "1.14") ]; out = "0.3.13"; } + { cases = [ (range "8.11" "8.14") (range "1.12" "1.13") ]; out = "0.3.10"; } + { cases = [ (range "8.11" "8.12") "1.11.0" ]; out = "0.3.4"; } + { cases = [ (range "8.10" "8.12") "1.11.0" ]; out = "0.3.3"; } + { cases = [ (range "8.10" "8.11") "1.11.0" ]; out = "0.3.1"; } + { cases = [ (range "8.8" "8.11") (range "1.8" "1.10") ]; out = "0.2.3"; } + ] null; + + # list of analysis packages sorted by dependency order + packages = [ "classical" "analysis" ]; + + mathcomp_ = package: let + classical-deps = [ mathcomp.algebra mathcomp-finmap hierarchy-builder ]; + analysis-deps = [ mathcomp.field mathcomp-bigenough ]; + intra-deps = if package == "single" then [] + else map mathcomp_ (head (splitList (lib.pred.equal package) packages)); + pkgpath = if package == "single" then "." + else if package == "analysis" then "theories" else "${package}"; + pname = if package == "single" then "mathcomp-analysis-single" + else "mathcomp-${package}"; + derivation = mkCoqDerivation ({ + inherit version pname defaultVersion release repo owner; + + namePrefix = [ "coq" "mathcomp" ]; + + propagatedBuildInputs = + intra-deps + ++ optionals (elem package [ "classical" "single" ]) classical-deps + ++ optionals (elem package [ "analysis" "single" ]) analysis-deps + ++ optionals (elem package [ "analysis" "single" ]) [mathcomp-algebra-tactics]; + + preBuild = '' + cd ${pkgpath} + ''; + + meta = { + description = "Analysis library compatible with Mathematical Components"; + maintainers = [ maintainers.cohencyril ]; + license = licenses.cecill-c; + }; + + passthru = genAttrs packages mathcomp_; + }); + # split packages didn't exist before 0.6, so bulding nothing in that case + patched-derivation1 = derivation.overrideAttrs (o: + optionalAttrs (o.pname != null && o.pname != "mathcomp-analysis" && + o.version != null && o.version != "dev" && versions.isLt "0.6" o.version) + { preBuild = ""; buildPhase = "echo doing nothing"; installPhase = "echo doing nothing"; } + ); + patched-derivation2 = patched-derivation1.overrideAttrs (o: + optionalAttrs (o.pname != null && o.pname == "mathcomp-analysis" && + o.version != null && o.version != "dev" && versions.isLt "0.6" o.version) + { preBuild = ""; } + ); + patched-derivation = patched-derivation2.overrideAttrs (o: + optionalAttrs (o.version != null + && (o.version == "dev" || versions.isGe "0.3.4" o.version)) + { + propagatedBuildInputs = o.propagatedBuildInputs ++ [ hierarchy-builder ]; + } + ); + in patched-derivation; +in +mathcomp_ (if single then "single" else "analysis") From 96c464761d6d520488976f38cd11af174e4319fa Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Thu, 24 Oct 2024 12:02:23 +0900 Subject: [PATCH 30/36] generalize case_sum --- theories/lang_syntax.v | 3 +- theories/lang_syntax_examples.v | 2 +- theories/lang_syntax_table_game.v | 4 +- theories/lang_syntax_toy.v | 2 +- theories/lang_syntax_util.v | 2 +- theories/prob_lang.v | 174 ++++++++++++++++-------------- 6 files changed, 99 insertions(+), 88 deletions(-) diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index d0be97a7a..5099b8d1b 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -1,6 +1,7 @@ Require Import String. From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. +From mathcomp Require Import lra. From mathcomp Require Import mathcomp_extra boolp classical_sets. From mathcomp Require Import functions cardinality fsbigop. From mathcomp Require Import signed reals ereal topology normedtype sequences. @@ -302,7 +303,7 @@ move=> x y x01 y01. by rewrite le_eqVlt => /predU1P[->//|/dF] => /(_ x01 y01)/ltW. Qed. -Lemma derive1_onem {R: realType} : (fun x0 : R => (1 - x0)%R : R^o)^`() = (cst (-1)%R). +Lemma derive1_onem {R : realType} : (fun x0 : R => (1 - x0)%R : R^o)^`() = (cst (-1)%R). Proof. apply/funext => x. by rewrite derive1E deriveB// derive_id derive_cst sub0r. diff --git a/theories/lang_syntax_examples.v b/theories/lang_syntax_examples.v index 666136fd2..41efab0ee 100644 --- a/theories/lang_syntax_examples.v +++ b/theories/lang_syntax_examples.v @@ -1,4 +1,4 @@ -Require Import String. +From Coq Require Import String. From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. From mathcomp.classical Require Import mathcomp_extra boolp. diff --git a/theories/lang_syntax_table_game.v b/theories/lang_syntax_table_game.v index d3477cd2c..dbd2c1a37 100644 --- a/theories/lang_syntax_table_game.v +++ b/theories/lang_syntax_table_game.v @@ -1,13 +1,13 @@ Require Import String. From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval. +From mathcomp Require Import ring lra. From mathcomp Require Import mathcomp_extra boolp classical_sets. From mathcomp Require Import functions cardinality fsbigop. From mathcomp Require Import signed reals ereal topology normedtype sequences. From mathcomp Require Import esum measure charge lebesgue_measure numfun. From mathcomp Require Import lebesgue_integral probability kernel prob_lang. From mathcomp Require Import lang_syntax_util lang_syntax lang_syntax_examples. -From mathcomp Require Import ring lra. (**md**************************************************************************) (* # Eddy's table game example *) @@ -100,7 +100,7 @@ rewrite ler_norml (@le_trans _ _ 0%R)//=. by rewrite onem_ge0. Unshelve. all: by end_near. Qed. -Lemma bounded_cst_01 (x : R) : [bounded x : R^o | _ in `[0%R, 1%R]%classic : set R]. +Lemma bounded_cst_01 (x : R^o) : [bounded x | _ in `[0%R, 1%R]%classic : set R]. Proof. exists `|x|%R; split. by rewrite num_real. diff --git a/theories/lang_syntax_toy.v b/theories/lang_syntax_toy.v index 149b7640c..ae6463adb 100644 --- a/theories/lang_syntax_toy.v +++ b/theories/lang_syntax_toy.v @@ -1,4 +1,4 @@ -Require Import String Classical. +From Coq Require Import String Classical. From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg. From mathcomp Require Import mathcomp_extra boolp. diff --git a/theories/lang_syntax_util.v b/theories/lang_syntax_util.v index 4e029ae27..253c1d8ff 100644 --- a/theories/lang_syntax_util.v +++ b/theories/lang_syntax_util.v @@ -1,4 +1,4 @@ -Require Import String. +From Coq Require Import String. From HB Require Import structures. Require Import Classical_Prop. (* NB: to compile with Coq 8.17 *) From mathcomp Require Import all_ssreflect. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index b4352847c..62c6cea84 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -2,6 +2,7 @@ From HB Require Import structures. From mathcomp Require Import all_ssreflect ssralg ssrnum ssrint interval finmap. From mathcomp Require Import rat archimedean. +From mathcomp Require Import lra. From mathcomp Require Import mathcomp_extra boolp classical_sets. From mathcomp Require Import functions cardinality fsbigop. From mathcomp Require Import reals ereal signed topology normedtype sequences. @@ -1003,7 +1004,7 @@ Definition case_nat_ m (xn : X * nat) : {measure set Y -> \bar R} := Let measurable_fun_case_nat_ m U : measurable U -> measurable_fun setT (case_nat_ m ^~ U). Proof. -move=> mU; rewrite /case_nat_ (_ : (fun _ => _) = +move=> mU/=; rewrite /case_nat_ (_ : (fun _ => _) = (fun x => if x.2 == m then k x.1 U else mzero U)) /=; last first. by apply/funext => -[t b]/=; case: ifPn. apply: (@measurable_fun_if_pair_nat _ _ _ _ (k ^~ U) (fun=> mzero U)) => //. @@ -1109,14 +1110,39 @@ Notation "p .-sum.-measurable" := ((p.-sum).-measurable : set (set (_ + _))) : classical_set_scope. +(* TODO: move *) +Lemma bigmaxEFin {R : realDomainType} {I : Type} (s : seq I) (P : I -> bool) + (F : I -> R) (x : R) : + (\big[Order.max/x%:E]_(i <- s | P i) (F i)%:E)%R = + (\big[Order.max/x]_(i <- s | P i) F i)%:E. +Proof. by rewrite (big_morph _ EFin_max erefl). Qed. + +#[short(type="measurableCountType")] +HB.structure Definition MeasurableCountable d := + {T of Measurable d T & Countable T }. + +#[short(type="measurableFinType")] +HB.structure Definition MeasurableFinite d := + {T of Measurable d T & Finite T }. + +Definition measurableTypeUnit := unit. + +HB.instance Definition _ := Pointed.on unit. +HB.instance Definition _ := MeasurableFinite.on measurableTypeUnit. + +Definition measurableTypeBool := bool. + +HB.instance Definition _ := Pointed.on measurableTypeBool. +HB.instance Definition _ := Finite.on measurableTypeBool. +HB.instance Definition _ := Measurable.on measurableTypeBool. + Module CASE_SUM. Section case_sum'. Section kcase_sum'. Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). -Let A : measurableType _ := unit. -Let B : measurableType _ := bool. +Context dA (A : measurableCountType dA) dB (B : measurableCountType dB). Variables (k1 : A -> R.-sfker X ~> Y) (k2 : B -> R.-sfker X ~> Y). Definition case_sum' : X * (A + B) -> {measure set Y -> \bar R} := @@ -1132,66 +1158,45 @@ rewrite /= => mU. apply: (measurability (ErealGenInftyO.measurableE R)) => //. move=> /= _ [_ [x ->] <-]; apply: measurableI => //. rewrite /case_sum'/= (_ : _ @^-1` _ = - ([set x1 | k1 tt x1 U < x%:E] `*` inl @` [set tt]) `|` - ([set x1 | k2 false x1 U < x%:E] `*` inr @` [set false]) `|` - ([set x1 | k2 true x1 U < x%:E] `*` inr @` [set true])); last first. + (\bigcup_a ([set x1 | k1 a x1 U < x%:E] `*` inl @` [set a])) `|` + (\bigcup_b ([set x1 | k2 b x1 U < x%:E] `*` inr @` [set b]))); last first. apply/seteqP; split. - - move=> z /=; rewrite in_itv/=; move: z => [z [[]|[|]]]//= ?. - + by do 2 left; split => //; exists tt. - + by right; split => //; exists true. - + by left; right; split => //; exists false. - - move=> z /=; rewrite in_itv/=; move: z => [z [[]|[|]]]//=. - - move=> [[[]//|]|]. - + by move=> [_ []]. - + by move=> [_ []]. - - move=> [[|]|[]//]. - + by move=> [_ []]. - + by move=> [_ [] [|]]. - - move=> [[|[]//]|]. - + by move=> [_ []]. - + by move=> [_ [] [|]]. -pose h1 := [set xub : X * (unit + bool) | k1 tt xub.1 U < x%:E]. -have mh1 : measurable h1. - rewrite -[X in measurable X]setTI; apply: emeasurable_fun_infty_o => //=. - have H : measurable_fun [set: X] (fun x => k1 tt x U) by exact/measurable_kernel. - move=> _ /= C mC; rewrite setTI. - have := H measurableT _ mC; rewrite setTI => {}H. - rewrite [X in measurable X](_ : _ = ((fun x => k1 tt x U) @^-1` C) `*` setT)//. - exact: measurableX. - by apply/seteqP; split => [z//=| z/= []]. -set h2 := [set xub : X * (unit + bool)| k2 false xub.1 U < x%:E]. -have mh2 : measurable h2. - rewrite -[X in measurable X]setTI. - apply: emeasurable_fun_infty_o => //=. - have H : measurable_fun [set: X] (fun x => k2 false x U) by exact/measurable_kernel. - move=> _ /= C mC; rewrite setTI. - have := H measurableT _ mC; rewrite setTI => {}H. - rewrite [X in measurable X](_ : _ = ((fun x => k2 false x U) @^-1` C) `*` setT)//. - exact: measurableX. - by apply/seteqP; split => [z //=|z/= []]. -set h3 := [set xub : X * (unit + bool)| k2 true xub.1 U < x%:E]. -have mh3 : measurable h3. - rewrite -[X in measurable X]setTI. - apply: emeasurable_fun_infty_o => //=. - have H : measurable_fun [set: X] (fun x => k2 (true) x U) by exact/measurable_kernel. - move=> _ /= C mC; rewrite setTI. - have := H measurableT _ mC; rewrite setTI => {}H. - rewrite [X in measurable X](_ : _ = ((fun x => k2 (true) x U) @^-1` C) `*` setT)//. - exact: measurableX. - by apply/seteqP; split=> [z//=|z/= []]. + - move=> z/=; rewrite in_itv/=. + move: z => [z [a|b]]/= ?. + + by left; exists a => //; split => //=; exists a. + + by right; exists b => //; split => //=; exists b. + - move=> z/=; rewrite in_itv/=. + move: z => [z [a|b]]/= [|]. + + by case => a' _ /= [] /[swap] [] [_ ->] [->]. + + by case => b' _ /= [] b'x [_ ->]. + + by case => b' _ /= [] b'x [_ ->]. + + by case => b' _ /= [] /[swap] [] [_ ->] [->]. apply: measurableU. -- apply: measurableU. - + apply: measurableX => //. - rewrite [X in measurable X](_ : _ = ysection h1 (inl tt))//. - * by apply: measurable_ysection. - * by apply/seteqP; split => z /=; rewrite /ysection /= inE. - + apply: measurableX => //. - rewrite [X in measurable X](_ : _ = ysection h2 (inr false))//. - * by apply: measurable_ysection. - * by apply/seteqP; split => z /=; rewrite /ysection /= inE. -- apply: measurableX => //. - rewrite [X in measurable X](_ : _ = ysection h3 (inr true))//. - + by apply: measurable_ysection. +- pose h1 a := [set xub : X * (A + B) | k1 a xub.1 U < x%:E]. + apply: countable_bigcupT_measurable; first exact: countableP. + move=> a; apply: measurableX => //. + rewrite [X in measurable X](_ : _ = ysection (h1 a) (inl a)). + + apply: (@measurable_ysection _ _ _ _ R). + rewrite -[X in measurable X]setTI. + apply: emeasurable_fun_infty_o => //= => _ /= C mC; rewrite setTI. + have : measurable_fun setT (fun x => k1 a x U) by exact/measurable_kernel. + move=> /(_ measurableT _ mC); rewrite setTI => H. + rewrite [X in measurable X](_ : _ = ((fun x => k1 a x U) @^-1` C) `*` setT)//. + exact: measurableX. + by apply/seteqP; split => [z//=| z/= []]. + + by apply/seteqP; split => z /=; rewrite /ysection /= inE. +- pose h2 a := [set xub : X * (A + B)| k2 a xub.1 U < x%:E]. + apply: countable_bigcupT_measurable; first exact: countableP. + move=> b; apply: measurableX => //. + rewrite [X in measurable X](_ : _ = ysection (h2 b) (inr b))//. + + apply: (@measurable_ysection _ _ _ _ R). + rewrite -[X in measurable X]setTI. + apply: emeasurable_fun_infty_o => //= _ /= C mC; rewrite setTI. + have : measurable_fun setT (fun x => k2 b x U) by exact/measurable_kernel. + move=> /(_ measurableT _ mC); rewrite setTI => H. + rewrite [X in measurable X](_ : _ = ((fun x => k2 b x U) @^-1` C) `*` setT)//. + exact: measurableX. + by apply/seteqP; split => [z //=|z/= []]. + by apply/seteqP; split => z /=; rewrite /ysection /= inE. Qed. @@ -1202,8 +1207,7 @@ End kcase_sum'. Section sfkcase_sum'. Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). -Let A : measurableType _ := unit. -Let B : measurableType _ := bool. +Context dA (A : measurableFinType dA) dB (B : measurableFinType dB). Variables (k1 : A -> R.-sfker X ~> Y) (k2 : B-> R.-sfker X ~> Y). Let sfinite_case_sum' : exists2 k_ : (R.-ker _ ~> _)^nat, @@ -1221,17 +1225,24 @@ set Hf2 := fun ab : B => svalP (cid (sfinite_kernel (k2 ab))). rewrite /= in Hf2. exists (fun n => case_sum' (f1 ^~ n) (f2 ^~ n)). move=> n /=. - have [rtt Hrtt] := measure_uub (f1 tt n). - have [rfalse Hrfalse] := measure_uub (f2 false n). - have [rtrue Hrtrue] := measure_uub (f2 true n). - exists (maxr rtt (maxr rfalse rtrue)) => //= -[x [[]|[|]]] /=. - by rewrite 2!EFin_max lt_max Hrtt. - by rewrite 2!EFin_max 2!lt_max Hrtrue 2!orbT. - by rewrite 2!EFin_max 2!lt_max Hrfalse orbT. -move=> [x [[]|[|]]] U mU/=-. -by rewrite (Hf1 tt x _ mU). -by rewrite (Hf2 true x _ mU). -by rewrite (Hf2 false x _ mU). + pose f1' a := sval (cid (measure_uub (f1 a n))). + pose f2' b := sval (cid (measure_uub (f2 b n))). + red. + exists (maxr (\big[Order.max/0%R]_a f1' a) (\big[Order.max/0%R]_b (f2' b)))%R. + move=> /= [x [a|b]]. + - have [bnd Hbnd] := measure_uub (f1 a n). + rewrite EFin_max lt_max; apply/orP; left. + rewrite /case_sum' -bigmaxEFin. + apply: lt_le_trans; last exact: le_bigmax_cond. + by rewrite /f1'; case: cid => /=. + - have [bnd Hbnd] := measure_uub (f2 b n). + rewrite EFin_max lt_max; apply/orP; right. + rewrite /case_sum' -bigmaxEFin. + apply: lt_le_trans; last exact: le_bigmax_cond. + by rewrite /f2'; case: cid => /=. +move=> [x [a|b]] U mU/=-. +- by rewrite (Hf1 a x _ mU). +- by rewrite (Hf2 b x _ mU). Qed. #[export] @@ -1243,10 +1254,8 @@ End case_sum'. Section case_sum. Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). -Let A : measurableType _ := unit. -Let B : measurableType _ := bool. +Context dA (A : measurableFinType dA) dB (B : measurableFinType dB). -(* case analysis on the datatype unit + bool *) Definition case_sum (f : R.-sfker X ~> (A + B)%type) (k1 : A -> R.-sfker X ~> Y) (k2 : B -> R.-sfker X ~> Y) : R.-sfker X ~> Y := f \; case_sum' k1 k2. @@ -1284,8 +1293,7 @@ End kcounting. (* formalization of the iterate construct of Staton ESOP 2017, Sect. 4.2 *) Section iterate. Context d {G : measurableType d} {R : realType}. -Let A : measurableType _ := unit. -Let B : measurableType _ := bool. +Context dA (A : measurableFinType dA) dB (B : measurableFinType dB). Import CASE_SUM. @@ -1304,7 +1312,7 @@ Fixpoint iterate_ n : R.-sfker G ~> B := (fun v => fail) end. -(* formalization of iterate (A = unit, B = bool) +(* formalization of iterate Gamma, x : A |-p t : A + B Gamma |-d u : A ----------------------------------------------- Gamma |-p iterate t from x = u : B *) @@ -1348,7 +1356,7 @@ Section von_neumann_trick. Context d {T : measurableType d} {R : realType}. Definition minltt {d1 d2} {T1 : measurableType d1} {T2 : measurableType d2} := - @measurable_cst _ _ T1 _ setT (@inl unit T2 tt). + @measurable_cst _ _ T1 _ setT (@inl _ T2 tt). Definition finrb d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) : T1 * bool -> T2 + bool := fun t1b => inr t1b.2. @@ -1367,8 +1375,10 @@ Definition trick : R.-sfker (T * unit) ~> (unit + bool)%type := (ite macc3of4 (letin (ret macc1of4) (ret minrb)) (ret minltt)))). +(* trick : R.-sfker T * unit ~> (unit + bool)%type *) -Definition von_neumann_trick : R.-sfker T ~> bool := iterate trick ktt. +Definition von_neumann_trick : R.-sfker T ~> measurableTypeBool := + (@iterate _ T R _ measurableTypeUnit _ measurableTypeBool trick _ ktt). End von_neumann_trick. From ece435551945386cd0c1cfaa0c238abb25bafda4 Mon Sep 17 00:00:00 2001 From: Cyril Cohen Date: Tue, 24 Dec 2024 03:29:16 +0100 Subject: [PATCH 31/36] Von Neumann Trick Proof complete --- .../mathcomp-analysis/default.nix | 317 +++++++++++++++--- classical/classical_sets.v | 4 + theories/measure.v | 13 + theories/prob_lang.v | 169 +++++++++- theories/probability.v | 28 ++ theories/sequences.v | 4 + 6 files changed, 470 insertions(+), 65 deletions(-) diff --git a/.nix/coq-overlays/mathcomp-analysis/default.nix b/.nix/coq-overlays/mathcomp-analysis/default.nix index 4e527a820..3a97b3d7c 100644 --- a/.nix/coq-overlays/mathcomp-analysis/default.nix +++ b/.nix/coq-overlays/mathcomp-analysis/default.nix @@ -1,14 +1,32 @@ -{ lib, - mkCoqDerivation, recurseIntoAttrs, - mathcomp, mathcomp-finmap, mathcomp-bigenough, - hierarchy-builder, mathcomp-algebra-tactics, +{ + lib, + mkCoqDerivation, + mathcomp, + mathcomp-finmap, + mathcomp-bigenough, + mathcomp-zify, + mathcomp-algebra-tactics, + hierarchy-builder, single ? false, - coqPackages, coq, version ? null }@args: -with builtins // lib; + coqPackages, + coq, + version ? null, +}@args: + let - repo = "analysis"; + repo = "analysis"; owner = "math-comp"; + release."1.7.0".sha256 = "sha256-GgsMIHqLkWsPm2VyOPeZdOulkN00IoBz++qA6yE9raQ="; + release."1.5.0".sha256 = "sha256-EWogrkr5TC5F9HjQJwO3bl4P8mij8U7thUGJNNI+k88="; + release."1.4.0".sha256 = "sha256-eDggeuEU0fMK7D5FbxvLkbAgpLw5lwL/Rl0eLXAnJeg="; + release."1.2.0".sha256 = "sha256-w6BivDM4dF4Iv4rUTy++2feweNtMAJxgGExPfYGhXxo="; + release."1.1.0".sha256 = "sha256-wl4kZf4mh9zbFfGcqaFEgWRyp0Bj511F505mYodpS6o="; + release."1.0.0".sha256 = "sha256-KiXyaWB4zQ3NuXadq4BSWfoN1cIo1xiLVSN6nW03tC4="; + release."0.7.0".sha256 = "sha256-JwkyetXrFsFHqz8KY3QBpHsrkhmEFnrCGuKztcoen60="; + release."0.6.7".sha256 = "sha256-3i2PBMEwihwgwUmnS0cmrZ8s+aLPFVq/vo0aXMUaUyA="; + release."0.6.6".sha256 = "sha256-tWtv6yeB5/vzwpKZINK9OQ0yQsvD8qu9zVSNHvLMX5Y="; + release."0.6.5".sha256 = "sha256-oJk9/Jl1SWra2aFAXRAVfX7ZUaDfajqdDksYaW8dv8E="; release."0.6.1".sha256 = "sha256-1VyNXu11/pDMuH4DmFYSUF/qZ4Bo+/Zl3Y0JkyrH/r0="; release."0.6.0".sha256 = "sha256-0msICcIrK6jbOSiBu0gIVU3RHwoEEvB88CMQqW/06rg="; release."0.5.3".sha256 = "sha256-1NjFsi5TITF8ZWx1NyppRmi8g6YaoUtTdS9bU/sUe5k="; @@ -23,40 +41,194 @@ let release."0.3.1".sha256 = "1iad288yvrjv8ahl9v18vfblgqb1l5z6ax644w49w9hwxs93f2k8"; release."0.2.3".sha256 = "0p9mr8g1qma6h10qf7014dv98ln90dfkwn76ynagpww7qap8s966"; - defaultVersion = with versions; lib.switch [ coq.version mathcomp.version ] [ - { cases = [ (isGe "8.14") (isGe "1.13.0") ]; out = "0.6.1"; } - { cases = [ (isGe "8.14") (range "1.13" "1.15") ]; out = "0.5.2"; } - { cases = [ (range "8.13" "8.15") (range "1.13" "1.14") ]; out = "0.5.1"; } - { cases = [ (range "8.13" "8.15") (range "1.12" "1.14") ]; out = "0.3.13"; } - { cases = [ (range "8.11" "8.14") (range "1.12" "1.13") ]; out = "0.3.10"; } - { cases = [ (range "8.11" "8.12") "1.11.0" ]; out = "0.3.4"; } - { cases = [ (range "8.10" "8.12") "1.11.0" ]; out = "0.3.3"; } - { cases = [ (range "8.10" "8.11") "1.11.0" ]; out = "0.3.1"; } - { cases = [ (range "8.8" "8.11") (range "1.8" "1.10") ]; out = "0.2.3"; } - ] null; + defaultVersion = + let + inherit (lib.versions) range; + in + lib.switch + [ coq.version mathcomp.version ] + [ + { + cases = [ + (range "8.19" "8.20") + (range "2.1.0" "2.2.0") + ]; + out = "1.7.0"; + } + { + cases = [ + (range "8.17" "8.20") + (range "2.0.0" "2.2.0") + ]; + out = "1.1.0"; + } + { + cases = [ + (range "8.17" "8.19") + (range "1.17.0" "1.19.0") + ]; + out = "0.7.0"; + } + { + cases = [ + (range "8.17" "8.18") + (range "1.15.0" "1.18.0") + ]; + out = "0.6.7"; + } + { + cases = [ + (range "8.17" "8.18") + (range "1.15.0" "1.18.0") + ]; + out = "0.6.6"; + } + { + cases = [ + (range "8.14" "8.18") + (range "1.15.0" "1.17.0") + ]; + out = "0.6.5"; + } + { + cases = [ + (range "8.14" "8.18") + (range "1.13.0" "1.16.0") + ]; + out = "0.6.1"; + } + { + cases = [ + (range "8.14" "8.18") + (range "1.13" "1.15") + ]; + out = "0.5.2"; + } + { + cases = [ + (range "8.13" "8.15") + (range "1.13" "1.14") + ]; + out = "0.5.1"; + } + { + cases = [ + (range "8.13" "8.15") + (range "1.12" "1.14") + ]; + out = "0.3.13"; + } + { + cases = [ + (range "8.11" "8.14") + (range "1.12" "1.13") + ]; + out = "0.3.10"; + } + { + cases = [ + (range "8.10" "8.12") + "1.11.0" + ]; + out = "0.3.3"; + } + { + cases = [ + (range "8.10" "8.11") + "1.11.0" + ]; + out = "0.3.1"; + } + { + cases = [ + (range "8.8" "8.11") + (range "1.8" "1.10") + ]; + out = "0.2.3"; + } + ] + null; # list of analysis packages sorted by dependency order - packages = [ "classical" "analysis" ]; + packages = { + "classical" = [ ]; + "reals" = [ "classical" ]; + "experimental-reals" = [ "reals" ]; + "analysis" = [ "reals" ]; + "reals-stdlib" = [ "reals" ]; + "analysis-stdlib" = [ + "analysis" + "reals-stdlib" + ]; + }; - mathcomp_ = package: let - classical-deps = [ mathcomp.algebra mathcomp-finmap hierarchy-builder ]; - analysis-deps = [ mathcomp.field mathcomp-bigenough ]; - intra-deps = if package == "single" then [] - else map mathcomp_ (head (splitList (lib.pred.equal package) packages)); - pkgpath = if package == "single" then "." - else if package == "analysis" then "theories" else "${package}"; - pname = if package == "single" then "mathcomp-analysis-single" - else "mathcomp-${package}"; + mathcomp_ = + package: + let + classical-deps = [ + mathcomp.algebra + mathcomp-finmap + mathcomp-zify + mathcomp-algebra-tactics + ]; + experimental-reals-deps = [ mathcomp-bigenough ]; + analysis-deps = [ + mathcomp.field + mathcomp-bigenough + ]; + intra-deps = lib.optionals (package != "single") (map mathcomp_ packages.${package}); + pkgpath = lib.switch package [ + { + case = "single"; + out = "."; + } + { + case = "analysis"; + out = "theories"; + } + { + case = "experimental-reals"; + out = "experimental_reals"; + } + { + case = "reals-stdlib"; + out = "reals_stdlib"; + } + { + case = "analysis-stdlib"; + out = "analysis_stdlib"; + } + ] package; + pname = if package == "single" then "mathcomp-analysis-single" else "mathcomp-${package}"; derivation = mkCoqDerivation ({ - inherit version pname defaultVersion release repo owner; + inherit + version + pname + defaultVersion + release + repo + owner + ; - namePrefix = [ "coq" "mathcomp" ]; + namePrefix = [ + "coq" + "mathcomp" + ]; propagatedBuildInputs = intra-deps - ++ optionals (elem package [ "classical" "single" ]) classical-deps - ++ optionals (elem package [ "analysis" "single" ]) analysis-deps - ++ optionals (elem package [ "analysis" "single" ]) [mathcomp-algebra-tactics]; + ++ lib.optionals (lib.elem package [ + "classical" + "single" + ]) classical-deps + ++ lib.optionals (lib.elem package [ + "experimental-reals" + "single" + ]) experimental-reals-deps + ++ lib.optionals (lib.elem package [ + "analysis" + "single" + ]) analysis-deps; preBuild = '' cd ${pkgpath} @@ -64,30 +236,65 @@ let meta = { description = "Analysis library compatible with Mathematical Components"; - maintainers = [ maintainers.cohencyril ]; - license = licenses.cecill-c; + maintainers = [ lib.maintainers.cohencyril ]; + license = lib.licenses.cecill-c; }; - passthru = genAttrs packages mathcomp_; + passthru = lib.mapAttrs (package: deps: mathcomp_ package) packages; }); - # split packages didn't exist before 0.6, so bulding nothing in that case - patched-derivation1 = derivation.overrideAttrs (o: - optionalAttrs (o.pname != null && o.pname != "mathcomp-analysis" && - o.version != null && o.version != "dev" && versions.isLt "0.6" o.version) - { preBuild = ""; buildPhase = "echo doing nothing"; installPhase = "echo doing nothing"; } - ); - patched-derivation2 = patched-derivation1.overrideAttrs (o: - optionalAttrs (o.pname != null && o.pname == "mathcomp-analysis" && - o.version != null && o.version != "dev" && versions.isLt "0.6" o.version) - { preBuild = ""; } - ); - patched-derivation = patched-derivation2.overrideAttrs (o: - optionalAttrs (o.version != null - && (o.version == "dev" || versions.isGe "0.3.4" o.version)) - { - propagatedBuildInputs = o.propagatedBuildInputs ++ [ hierarchy-builder ]; - } - ); - in patched-derivation; + # split packages didn't exist before 0.6, so bulding nothing in that case + patched-derivation1 = derivation.overrideAttrs ( + o: + lib.optionalAttrs + ( + o.pname != null + && o.pname != "mathcomp-analysis" + && o.version != null + && o.version != "dev" + && lib.versions.isLt "0.6" o.version + ) + { + preBuild = ""; + buildPhase = "echo doing nothing"; + installPhase = "echo doing nothing"; + } + ); + patched-derivation2 = patched-derivation1.overrideAttrs ( + o: + lib.optionalAttrs ( + o.pname != null + && o.pname == "mathcomp-analysis" + && o.version != null + && o.version != "dev" + && lib.versions.isLt "0.6" o.version + ) { preBuild = ""; } + ); + # only packages classical and analysis existed before 1.7, so bulding nothing in that case + patched-derivation3 = patched-derivation2.overrideAttrs ( + o: + lib.optionalAttrs + ( + o.pname != null + && o.pname != "mathcomp-classical" + && o.pname != "mathcomp-analysis" + && o.version != null + && o.version != "dev" + && lib.versions.isLt "1.7" o.version + ) + { + preBuild = ""; + buildPhase = "echo doing nothing"; + installPhase = "echo doing nothing"; + } + ); + patched-derivation = patched-derivation3.overrideAttrs ( + o: + lib.optionalAttrs (o.version != null && (o.version == "dev" || lib.versions.isGe "0.3.4" o.version)) + { + propagatedBuildInputs = o.propagatedBuildInputs ++ [ hierarchy-builder ]; + } + ); + in + patched-derivation; in mathcomp_ (if single then "single" else "analysis") diff --git a/classical/classical_sets.v b/classical/classical_sets.v index 86b9b0ebe..4331a45b6 100644 --- a/classical/classical_sets.v +++ b/classical/classical_sets.v @@ -550,6 +550,7 @@ Qed. Notation setTP := setTPn (only parsing). Lemma in_set0 (x : T) : (x \in set0) = false. Proof. by rewrite memNset. Qed. + Lemma in_setT (x : T) : x \in setT. Proof. by rewrite mem_set. Qed. Lemma in_setC (x : T) A : (x \in ~` A) = (x \notin A). @@ -2224,6 +2225,9 @@ Notation bigcap_set := bigcap_seq (only parsing). #[deprecated(since="mathcomp-analysis 0.6.4",note="Use bigcap_seq_cond instead")] Notation bigcap_set_cond := bigcap_seq_cond (only parsing). +Lemma in_set1 [T : finType] (x y : T) : (x \in [set y]) = (x \in [set y]%SET). +Proof. by apply/idP/idP; rewrite !inE /= => /eqP. Qed. + Lemma bigcup_pred [T : finType] [U : Type] (P : {pred T}) (f : T -> set U) : \bigcup_(t in [set` P]) f t = \big[setU/set0]_(t in P) f t. Proof. diff --git a/theories/measure.v b/theories/measure.v index 0a3b817a0..0d55330d6 100644 --- a/theories/measure.v +++ b/theories/measure.v @@ -3534,6 +3534,12 @@ move=> mA; rewrite -(@probability_setT _ _ _ P). by apply: le_measure => //; rewrite ?in_setE. Qed. +Lemma probability_fin (A : set T) : measurable A -> P A \is a fin_num. +Proof. +move=> Ameas; rewrite ge0_fin_numE//. +by rewrite (@le_lt_trans _ _ 1)// ?measure_ge0 ?probability_le1// ?ltey. +Qed. + Lemma probability_setC (A : set T) : measurable A -> P (~` A) = 1 - P A. Proof. move=> mA. @@ -3603,6 +3609,13 @@ HB.instance Definition _ := End mnormalize. +Lemma mnormalize_id d (T : measurableType d) (R : realType) + (P P' : probability T R) : mnormalize P P' = P. +Proof. +apply/funext => x; rewrite /mnormalize/= probability_setT. +by rewrite onee_eq0/= invr1 mule1. +Qed. + Section pdirac. Context d (T : measurableType d) (R : realType). diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 62c6cea84..0807eaee6 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -7,7 +7,7 @@ From mathcomp Require Import mathcomp_extra boolp classical_sets. From mathcomp Require Import functions cardinality fsbigop. From mathcomp Require Import reals ereal signed topology normedtype sequences. From mathcomp Require Import esum measure lebesgue_measure numfun. -From mathcomp Require Import lebesgue_integral probability exp kernel charge. +From mathcomp Require Import lebesgue_integral probability exp kernel. From mathcomp Require Import ring lra. (**md**************************************************************************) @@ -1127,8 +1127,9 @@ HB.structure Definition MeasurableFinite d := Definition measurableTypeUnit := unit. -HB.instance Definition _ := Pointed.on unit. -HB.instance Definition _ := MeasurableFinite.on measurableTypeUnit. +HB.instance Definition _ := Pointed.on measurableTypeUnit. +HB.instance Definition _ := Finite.on measurableTypeUnit. +HB.instance Definition _ := Measurable.on measurableTypeUnit. Definition measurableTypeBool := bool. @@ -1176,7 +1177,7 @@ apply: measurableU. apply: countable_bigcupT_measurable; first exact: countableP. move=> a; apply: measurableX => //. rewrite [X in measurable X](_ : _ = ysection (h1 a) (inl a)). - + apply: (@measurable_ysection _ _ _ _ R). + + apply: measurable_ysection. rewrite -[X in measurable X]setTI. apply: emeasurable_fun_infty_o => //= => _ /= C mC; rewrite setTI. have : measurable_fun setT (fun x => k1 a x U) by exact/measurable_kernel. @@ -1184,12 +1185,12 @@ apply: measurableU. rewrite [X in measurable X](_ : _ = ((fun x => k1 a x U) @^-1` C) `*` setT)//. exact: measurableX. by apply/seteqP; split => [z//=| z/= []]. - + by apply/seteqP; split => z /=; rewrite /ysection /= inE. + + by rewrite ysectionE. - pose h2 a := [set xub : X * (A + B)| k2 a xub.1 U < x%:E]. apply: countable_bigcupT_measurable; first exact: countableP. move=> b; apply: measurableX => //. rewrite [X in measurable X](_ : _ = ysection (h2 b) (inr b))//. - + apply: (@measurable_ysection _ _ _ _ R). + + apply: measurable_ysection. rewrite -[X in measurable X]setTI. apply: emeasurable_fun_infty_o => //= _ /= C mC; rewrite setTI. have : measurable_fun setT (fun x => k2 b x U) by exact/measurable_kernel. @@ -1197,7 +1198,7 @@ apply: measurableU. rewrite [X in measurable X](_ : _ = ((fun x => k2 b x U) @^-1` C) `*` setT)//. exact: measurableX. by apply/seteqP; split => [z //=|z/= []]. - + by apply/seteqP; split => z /=; rewrite /ysection /= inE. + + by rewrite ysectionE. Qed. #[export] @@ -1367,6 +1368,8 @@ Proof. exact: measurableT_comp. Qed. (* biased coin *) Variable (D : pprobability bool R). +Let unit := measurableTypeUnit. +Let bool := measurableTypeBool. Definition trick : R.-sfker (T * unit) ~> (unit + bool)%type := letin (sample_cst D) @@ -1375,10 +1378,156 @@ Definition trick : R.-sfker (T * unit) ~> (unit + bool)%type := (ite macc3of4 (letin (ret macc1of4) (ret minrb)) (ret minltt)))). -(* trick : R.-sfker T * unit ~> (unit + bool)%type *) -Definition von_neumann_trick : R.-sfker T ~> measurableTypeBool := - (@iterate _ T R _ measurableTypeUnit _ measurableTypeBool trick _ ktt). +Definition von_neumann_trick_kernel : _ -> _ := + (@iterate _ _ R _ unit _ bool trick _ ktt). +Definition von_neumann_trick x : _ -> _ := von_neumann_trick_kernel x. + +HB.instance Definition _ := SFiniteKernel.on von_neumann_trick_kernel. +HB.instance Definition _ x := Measure.on (von_neumann_trick x). + +Section von_neumann_trick_proof. + +Hypothesis D_nontrivial : 0 < D [set true] < 1. + +Let p : R := fine (D [set true]). +Let q : R := p * (1 - p). +Let r : R := p ^+ 2 + (1 - p) ^+ 2. + +Let Dtrue : D [set true] = p%:E. +Proof. by rewrite fineK//= probability_fin. Qed. + +Let Dfalse : D [set false] = 1 - (p%:E). +Proof. +rewrite -Dtrue; have <- : D [set: bool] = 1 := probability_setT. +rewrite setT_bool measureU//=; last first. + by rewrite disjoints_subset => -[]//. +by rewrite addeAC subee ?probability_fin ?add0e. +Qed. + +Let p_ge0 : (0 <= p)%R. +Proof. by rewrite fine_ge0 ?measure_ge0. Qed. + +Let p'_ge0 : (1 - p >= 0)%R. +Proof. by rewrite -lee_fin EFinB -Dfalse measure_ge0. Qed. + +Let q_ge0 : (q >= 0)%R. Proof. by rewrite mulr_ge0 ?p_ge0 ?p'_ge0. Qed. + +Let r_ge0 : (r >= 0)%R. +Proof. by rewrite addr_ge0// exprn_ge0 ?p_ge0 ?p'_ge0. Qed. + +Let intDE f : (forall x, 0 <= f x) -> + \int[D]_x (f x) = (1 - (p%:E)) * f false + p%:E * f true. +Proof. +move=> f_ge0. +pose M := measure_add (mscale (NngNum p'_ge0) \d_false) + (mscale (NngNum p_ge0) \d_true). +rewrite (eq_measure_integral M) => [|A Ameas _]. + rewrite ge0_integral_measure_add//. + by rewrite !ge0_integral_mscale ?integral_dirac ?diracT ?mul1e. +rewrite /M/= /msum/= !big_ord_recl/= big_ord0 addr0 /mscale/=. +by case: (set_bool A) => /eqP->/=; + rewrite ?measure0 ?probability_setT ?Dtrue ?Dfalse//= + ?diracE/= ?in_set1 ?inE/= ?(mule0, mul0e, adde0, add0e, mule1)// + -EFinD addrNK. +Qed. + +Let p_gt0 : (0 < p)%R. +Proof. by rewrite -lte_fin -Dtrue; case/andP : D_nontrivial. Qed. + +Let p_lt1 : (p < 1)%R. +Proof. by rewrite -lte_fin -Dtrue; case/andP : D_nontrivial. Qed. + +Let p'_gt0 : (0 < 1 - p)%R. Proof. by rewrite subr_gt0. Qed. + +Let r_lt1 : (r < 1)%R. +Proof. +rewrite /r -subr_gt0 [ltRHS](_ : _ = 2 * p * (1 - p))%R; last by ring. +by rewrite !mulr_gt0. +Qed. + +Lemma iterate_trick gamma n (b : bool) : + @iterate_ _ _ R _ unit _ bool trick _ ktt n gamma [set b] + = (geometric q r n)%:E. +Proof. +elim: n => [|n IHn] //=; rewrite /kcomp; rewrite integral_kcomp//=; + rewrite integral_dirac//= ?diracT ?mul1e integral_kcomp//=; + under eq_integral do [rewrite integral_kcomp//=; + under eq_integral do rewrite integral_kcomp//= + ?integral_dirac//= ?diracT ?mul1e]; + do ![rewrite intDE//=; last by + [move=> b'; do !rewrite integral_ge0//= => *]]; + rewrite !iteE//= ?integral_kcomp//= + ?integral_dirac//= ?diracT ?mul1e/=; + rewrite /acc1of4/= /kcomp ?ge0_integral_mscale //= + ?diracE/= ?in_set1 ?inE/= + ?(mule0, mul0e, adde0, add0e, mule1, normr0)//=; + rewrite -?(EFinB, EFinN, EFinM, EFinD) ?lee_fin ?expr0 ?mulr1//. + by case: b => //=; rewrite ?(mulr0, mulr1, add0r, addr0)// ?[(_ * p)%R]mulrC. +rewrite IHn -?(EFinB, EFinN, EFinM, EFinD) ?lee_fin ?expr0 ?mulr1//. +rewrite [geometric _ _ _]lock !mulrA -mulrDl addrC. +by rewrite /geometric/= -/r -lock mulrCA exprS. +Qed. + +Lemma iterate_trickT gamma n : + @iterate_ _ _ R _ unit _ bool trick _ ktt n gamma [set: bool] = + (2 * geometric q r n)%:E. +Proof. +rewrite setT_bool measureU//=; last first. + by rewrite disjoints_subset => -[]//. +by rewrite !iterate_trick -EFinD -mulr2n mulr_natl. +Qed. + +Lemma von_neumann_trick_prob_kernel gamma b : + von_neumann_trick_kernel gamma [set b] = 2^-1%:E. +Proof. +rewrite /= /kcomp/= /case_nat_/= /mseries. +under eq_integral => n _. + under (@congr_lim _ _ _ \o @eq_fun _ _ _ _) => k. + under eq_bigr do rewrite fun_if/= (fun_if (@^~ _))/mzero eq_sym. + rewrite -big_mkcond/= big_nat1_eq iterate_trick. + over. +over. +rewrite /= (eq_integral (EFin \o geometric q r))//=; last first. + by move=> k _; apply/lim_near_cst => //; near do rewrite ifT//. +have cvgg: series (geometric q r) x @[x --> \oo] --> (q / (1 - r))%R. + by apply/cvg_geometric_series; rewrite ger0_norm ?r_lt1. +have limgg := cvg_lim (@Rhausdorff R) cvgg. +have sumgE : \big[+%R/0%R]_(0 <= k *; field. + rewrite [X in X != _](_ : _ = 2 * (p * (1 - p)))%R; last by ring. + by rewrite mulf_eq0 ?pnatr_eq0/= mulf_neq0// gt_eqF ?p_gt0 ?p'_gt0//. +suff summableg : summable setT (EFin \o geometric q r) + by rewrite integral_count. +rewrite /summable /=. +rewrite (_ : [set: nat] = [set x | x \in predT]); last first. + by apply/eq_set => x; rewrite inE trueE. +rewrite -(@nneseries_esum _ _ predT)//=. +under eq_eseriesr do rewrite ger0_norm// ?geometric_ge0//. +by rewrite sumgE ltey. +Unshelve. all: end_near. Qed. + +Lemma von_neumann_trick_prob_kernelT gamma : + von_neumann_trick gamma [set: bool] = 1. +Proof. +rewrite setT_bool measureU//=; last first. + by rewrite disjoints_subset => -[]. +rewrite !von_neumann_trick_prob_kernel -EFinD. +by have := splitr (1 : R); rewrite mul1r => <-. +Qed. + +HB.instance Definition _ gamma := Measure.on (von_neumann_trick gamma). +HB.instance Definition _ gamma := Measure_isProbability.Build _ _ _ + (von_neumann_trick gamma) (von_neumann_trick_prob_kernelT gamma). +HB.instance Definition _ := Kernel_isProbability.Build _ _ _ _ _ + von_neumann_trick_kernel von_neumann_trick_prob_kernelT. + +Theorem von_neumann_trickP gamma : von_neumann_trick gamma =1 bernoulli 2^-1. +Proof. by apply: eq_bernoulli; rewrite von_neumann_trick_prob_kernel. Qed. + +End von_neumann_trick_proof. End von_neumann_trick. diff --git a/theories/probability.v b/theories/probability.v index 1ff17ee46..2909baf6f 100644 --- a/theories/probability.v +++ b/theories/probability.v @@ -937,6 +937,23 @@ Qed. HB.instance Definition _ := @Measure_isProbability.Build _ _ R bernoulli bernoulli_setT. +Lemma eq_bernoulli (P : probability bool R) : + P [set true] = p%:E -> P =1 bernoulli. +Proof. +move=> Ptrue sb; rewrite /bernoulli /bernoulli_pmf. +have Pfalse: P [set false] = (1 - p%:E)%E. + rewrite -Ptrue -(@probability_setT _ _ _ P) setT_bool measureU//; last first. + by rewrite disjoints_subset => -[]//. + by rewrite addeAC subee ?add0e//= Ptrue. +have: (0 <= p%:E <= 1)%E by rewrite -Ptrue measure_ge0 probability_le1. +rewrite !lee_fin => ->. +have eq_sb := etrans (bigcup_imset1 (_ : set bool) id) (image_id _). +rewrite -[in LHS](eq_sb sb)/= measure_fin_bigcup//; last 2 first. +- exact: finite_finset. +- by move=> [] [] _ _ [[]]//= []. +- by apply: eq_fsbigr => /= -[]. +Qed. + End bernoulli. Section bernoulli_measure. @@ -966,6 +983,17 @@ Qed. End bernoulli_measure. Arguments bernoulli {R}. +Lemma eq_bernoulliV2 {R : realType} (P : probability bool R) : + P [set true] = P [set false] -> P =1 bernoulli 2^-1. +Proof. +move=> Ptrue_eq_false; apply/eq_bernoulli. +have : P [set: bool] = 1%E := probability_setT. +rewrite setT_bool measureU//=; last first. + by rewrite disjoints_subset => -[]//. +rewrite Ptrue_eq_false -mule2n; move/esym/eqP. +by rewrite -mule_natl -eqe_pdivrMl// mule1 => /eqP<-. +Qed. + Section integral_bernoulli. Context {R : realType}. Variables (p : R) (p01 : (0 <= p <= 1)%R). diff --git a/theories/sequences.v b/theories/sequences.v index 220622036..ac185a2d6 100644 --- a/theories/sequences.v +++ b/theories/sequences.v @@ -1004,6 +1004,10 @@ rewrite exprDn (bigD1 (inord 1)) ?inordK// subn1 expr1 bin1 lerDl sumr_ge0//. by move=> i; rewrite ?(mulrn_wge0, mulr_ge0, exprn_ge0, subr_ge0)// ltW. Unshelve. all: by end_near. Qed. +Lemma geometric_ge0 (R : numFieldType) (a z : R) n : + 0 <= a -> 0 <= z -> geometric a z n >= 0. +Proof. by move=> *; rewrite mulr_ge0// exprn_ge0. Qed. + Lemma geometric_seriesE (R : numFieldType) (a z : R) : z != 1 -> series (geometric a z) = [sequence a * (1 - z ^+ n) / (1 - z)]_n. Proof. From 599d7a251833392e5d0aec4b05c096f2fab2e01f Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Fri, 27 Dec 2024 14:10:03 +0900 Subject: [PATCH 32/36] probability_fin can be avoided --- theories/measure.v | 6 ------ theories/prob_lang.v | 8 ++++---- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/theories/measure.v b/theories/measure.v index 0d55330d6..7b0d26b45 100644 --- a/theories/measure.v +++ b/theories/measure.v @@ -3534,12 +3534,6 @@ move=> mA; rewrite -(@probability_setT _ _ _ P). by apply: le_measure => //; rewrite ?in_setE. Qed. -Lemma probability_fin (A : set T) : measurable A -> P A \is a fin_num. -Proof. -move=> Ameas; rewrite ge0_fin_numE//. -by rewrite (@le_lt_trans _ _ 1)// ?measure_ge0 ?probability_le1// ?ltey. -Qed. - Lemma probability_setC (A : set T) : measurable A -> P (~` A) = 1 - P A. Proof. move=> mA. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 0807eaee6..9bc329960 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -314,7 +314,7 @@ rewrite (_ : (fun x => _) = (fun x => x * apply/funext => x; case: ifPn => ix; first by rewrite indicE/= mem_set ?mule1. by rewrite indicE/= memNset ?mule0// /= in_itv/=; exact/negP. apply: emeasurable_funM => //=; apply/measurable_EFinP. -by rewrite (_ : \1__ = mindic R (emeasurable_itv `[(i%:R)%:E, (i.+1%:R)%:E[)). +by rewrite (_ : \1__ = mindic R (emeasurable_itv `[i%:R%:E, i.+1%:R%:E[)). Qed. Definition mk i t := [the measure _ _ of k mf i t]. @@ -1395,14 +1395,14 @@ Let q : R := p * (1 - p). Let r : R := p ^+ 2 + (1 - p) ^+ 2. Let Dtrue : D [set true] = p%:E. -Proof. by rewrite fineK//= probability_fin. Qed. +Proof. by rewrite fineK//= fin_num_measure. Qed. Let Dfalse : D [set false] = 1 - (p%:E). Proof. rewrite -Dtrue; have <- : D [set: bool] = 1 := probability_setT. rewrite setT_bool measureU//=; last first. by rewrite disjoints_subset => -[]//. -by rewrite addeAC subee ?probability_fin ?add0e. +by rewrite addeAC subee ?fin_num_measure ?add0e. Qed. Let p_ge0 : (0 <= p)%R. @@ -1510,7 +1510,7 @@ by rewrite sumgE ltey. Unshelve. all: end_near. Qed. Lemma von_neumann_trick_prob_kernelT gamma : - von_neumann_trick gamma [set: bool] = 1. + von_neumann_trick gamma [set: bool] = 1. Proof. rewrite setT_bool measureU//=; last first. by rewrite disjoints_subset => -[]. From 822b357ac45562f1f50547afa8ab4b6dfc322b8c Mon Sep 17 00:00:00 2001 From: Cyril Cohen Date: Sun, 29 Dec 2024 21:33:45 +0100 Subject: [PATCH 33/36] Better Von Neumann trick + generalization --- classical/classical_sets.v | 19 ++++ classical/mathcomp_extra.v | 6 + reals/constructive_ereal.v | 3 + theories/measure.v | 8 ++ theories/prob_lang.v | 228 ++++++++++++++++++++++++++----------- 5 files changed, 198 insertions(+), 66 deletions(-) diff --git a/classical/classical_sets.v b/classical/classical_sets.v index 4331a45b6..95265327e 100644 --- a/classical/classical_sets.v +++ b/classical/classical_sets.v @@ -1439,9 +1439,15 @@ Implicit Types (A B : set aT) (f : aT -> rT) (Y : set rT). Lemma imageP f A a : A a -> (f @` A) (f a). Proof. by exists a. Qed. +Lemma image_f f A a : a \in A -> f a \in [set f x | x in A]. +Proof. by rewrite !inE; apply/imageP. Qed. + Lemma imageT (f : aT -> rT) (a : aT) : range f (f a). Proof. by apply: imageP. Qed. +Lemma mem_range f a : f a \in range f. +Proof. by rewrite !inE; apply/imageT. Qed. + End base_image_lemmas. #[global] Hint Extern 0 ((?f @` _) (?f _)) => solve [apply: imageP; assumption] : core. @@ -1456,6 +1462,10 @@ Proof. by move=> f_inj; rewrite propeqE; split => [[b Ab /f_inj <-]|/(imageP f)//]. Qed. +Lemma mem_image {f A a} : injective f -> + (f a \in [set f x | x in A]) = (a \in A). +Proof. by move=> /image_inj finj; apply/idP/idP; rewrite !inE finj. Qed. + Lemma image_id A : id @` A = A. Proof. by rewrite eqEsubset; split => a; [case=> /= x Ax <-|exists a]. Qed. @@ -1730,6 +1740,15 @@ Proof. by apply/disj_setPS/disj_setPS; rewrite -some_setI -some_set0 sub_image_someP. Qed. + +Lemma inl_in_set_inr A B (x : A) (Y : set B) : + inl x \in [set inr y | y in Y] = false. +Proof. by apply/negP; rewrite inE/= => -[]. Qed. + +Lemma inr_in_set_inr A B (y : B) (Y : set B) : + inr y \in [set @inr A B y | y in Y] = (y \in Y). +Proof. by apply/idP/idP => [/[!inE][/= [x ? [<-]]]|/[!inE]]//; exists y. Qed. + Section bigop_lemmas. Context {T I : Type}. Implicit Types (A : set T) (i : I) (P : set I) (F G : I -> set T). diff --git a/classical/mathcomp_extra.v b/classical/mathcomp_extra.v index 7f5eeb3af..c8637e37f 100644 --- a/classical/mathcomp_extra.v +++ b/classical/mathcomp_extra.v @@ -588,3 +588,9 @@ rewrite mulr_ile1 ?andbT//. by have := xs01 x; rewrite inE xs orbT => /(_ _)/andP[]. by rewrite ih// => e xs; rewrite xs01// in_cons xs orbT. Qed. + +Lemma inr_inj {A B} : injective (@inr A B). +Proof. by move=> ? ? []. Qed. + +Lemma inl_inj {A B} : injective (@inl A B). +Proof. by move=> ? ? []. Qed. diff --git a/reals/constructive_ereal.v b/reals/constructive_ereal.v index a119e963f..348fa231f 100644 --- a/reals/constructive_ereal.v +++ b/reals/constructive_ereal.v @@ -670,6 +670,9 @@ Definition fin_num := [qualify a x : \bar R | (x != -oo) && (x != +oo)]. Fact fin_num_key : pred_key fin_num. Proof. by []. Qed. (*Canonical fin_num_keyd := KeyedQualifier fin_num_key.*) +Lemma fin_numP_EFin x : reflect (exists r, x = r%:E) (x \in fin_num). +Proof. by case: x => [r'||]//=; constructor; [exists r'| case | case ]. Qed. + Lemma fin_numE x : (x \is a fin_num) = (x != -oo) && (x != +oo). Proof. by []. Qed. diff --git a/theories/measure.v b/theories/measure.v index 7b0d26b45..6b8898697 100644 --- a/theories/measure.v +++ b/theories/measure.v @@ -1507,6 +1507,12 @@ by move=> fg mf mD A mA; rewrite [X in measurable X](_ : _ = D `&` f @^-1` A); [exact: mf|exact/esym/eq_preimage]. Qed. +Lemma measurable_fun_eqP D (f g : T1 -> T2) : + {in D, f =1 g} -> measurable_fun D f <-> measurable_fun D g. +Proof. +by move=> eq_fg; split; apply/eq_measurable_fun => // ? ?; rewrite eq_fg. +Qed. + Lemma measurable_cst D (r : T2) : measurable_fun D (cst r : T1 -> _). Proof. by move=> mD /= Y mY; rewrite preimage_cst; case: ifPn; rewrite ?setIT ?setI0. @@ -1574,6 +1580,8 @@ End measurable_fun. solve [apply: measurable_id] : core. Arguments eq_measurable_fun {d1 d2 T1 T2 D} f {g}. #[deprecated(since="mathcomp-analysis 0.6.2", note="renamed `eq_measurable_fun`")] +Arguments measurable_fun_eqP {d1 d2 T1 T2 D} f {g}. +#[deprecated(since="mathcomp-analysis 0.6.2", note="renamed `eq_measurable_fun`")] Notation measurable_fun_ext := eq_measurable_fun (only parsing). #[deprecated(since="mathcomp-analysis 0.6.3", note="renamed `measurable_id`")] Notation measurable_fun_id := measurable_id (only parsing). diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 9bc329960..70b89df55 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -1321,6 +1321,131 @@ Definition iterate : R.-sfker G ~> B := case_nat (kcounting R) iterate_. End iterate. +Section iterate_unit. + +Let unit := measurableTypeUnit. +Let bool := measurableTypeBool. +Context d {G : measurableType d} {R : realType}. +Context dB (B : measurableFinType dB). + +Section iterate_elim. +Variables (t : R.-sfker (G * unit) ~> (unit + B)%type) + (u : G -> unit) (mu : measurable_fun setT u). +Variables (r : R) (tlE : forall gamma, t (gamma, tt) [set inl tt] = r%:E). + +Variables (gamma : G) (X : set B) (q : R). +Hypothesis trE : t (gamma, tt) [set inr x | x in X] = q%:E. + +Let q_ge0 : (0 <= q)%R. Proof. by rewrite -lee_fin -trE measure_ge0. Qed. +Let r_ge0 : (0 <= r)%R. +Proof. by rewrite -lee_fin -(tlE gamma) measure_ge0. Qed. + +Lemma iterate_E n : iterate_ t mu n gamma X = (geometric q r n)%:E. +Proof. +elim: n => [|n IHn] //=; + rewrite /kcomp; rewrite integral_kcomp//=; + rewrite /= integral_dirac//= ?diracT ?mul1e ?expr0 ?exprS ?mulr1. + rewrite (eq_integral (EFin \o \1_[set inr x | x in X]))//=; last first. + move=> [a' _|b _]//=; last first. + by rewrite diracE indicE/= (mem_image inr_inj). + rewrite /kcomp/= indicE /= ge0_integral_mscale//= normr0 mul0e. + by rewrite [_ \in _](introF idP)// inE /= => -[]. + by rewrite ?unitE integral_indic//= setIT. +pose g : unit + B -> R^o := (geometric q r n \o* \1_[set inl tt])%R. +rewrite (eq_integral (EFin \o g))//=; last first. + move=> [[] _|b _]//=. + by rewrite /g/= indicE//= in_set1 inE eqxx mul1r. + rewrite /kcomp/= ge0_integral_mscale//= normr0 mul0e. + by rewrite /g /= indicE//= in_set1 inE mul0r. +rewrite /g /=; under eq_integral do rewrite EFinM. +rewrite integralZr//=; last first. + apply/integrableP; split=> //. + under eq_integral => x. + rewrite gee0_abs//=; last first. + by rewrite indicE lee_fin natr_ge0//. + over. + by rewrite /= integral_indic// setIT [u gamma]unitE tlE ltey. +by rewrite integral_indic//= [u gamma]unitE setIT tlE -EFinM mulrCA. +Qed. + +Hypothesis r_lt1 : (r < 1)%R. + +Lemma iterateE : iterate t mu gamma X = (q / (1 - r))%:E. +Proof. +rewrite /= /kcomp/= /case_nat_/= /mseries. +under eq_integral => n _. + under (@congr_lim _ _ _ \o @eq_fun _ _ _ _) => k. + under eq_bigr do rewrite fun_if/= (fun_if (@^~ _))/mzero eq_sym. + rewrite -big_mkcond/= big_nat1_eq. + over. +over. +rewrite /= (eq_integral (EFin \o geometric q r))//=; last first. + move=> k _; apply/lim_near_cst => //; rewrite iterate_E ?r_ge0 ?r_lt1//. + by near do rewrite ifT//. +have cvgg: series (geometric q r) x @[x --> \oo] --> (q / (1 - r))%R. + by apply/cvg_geometric_series; rewrite ger0_norm ?r_lt1//. +have limgg := cvg_lim (@Rhausdorff R) cvgg. +have sumgE : \big[+%R/0%R]_(0 <= k x; rewrite inE trueE. +rewrite -(@nneseries_esum _ _ predT)//=. +under eq_eseriesr do rewrite ger0_norm// ?geometric_ge0//. +by rewrite sumgE ltey. +Unshelve. all: end_near. Qed. + +End iterate_elim. + +Import CASE_SUM. + +Variables (t : R.-pker (G * unit) ~> (unit + B)%type) + (u : G -> unit) (mu : measurable_fun setT u). +Variables (r : R) (r_lt1 : (r < 1)%R). +Hypothesis (tlE : forall gamma, t (gamma, tt) [set inl tt] = r%:E). + +Let trE gamma X : t (gamma, tt) [set inr x | x in X] \in fin_num. +Proof. +apply/fin_numPlt; rewrite (@lt_le_trans _ _ 0)//=. +rewrite (@le_lt_trans _ _ 1)//= ?ltey//. +rewrite -( @prob_kernel _ _ _ _ _ t (gamma, tt) ). +by apply/le_measure => //=; rewrite inE//=. +Qed. + +Lemma iterate_normalize p : + iterate t mu = knormalize (case_sum (letin (ret mu) t) + (fun u' => fail) + (fun v => ret (measurable_cst v))) p. +Proof. +apply/eq_sfkernel => gamma U. +have /fin_numP_EFin[q trE'] := trE gamma U. +rewrite (iterateE mu tlE trE')//; symmetry. +rewrite /= /mnormalize/= (fun_if (@^~ U))/=. +set m := kcomp _ _ _. +have mE V : m V = t (gamma, tt) [set inr x | x in V]. + rewrite /m/= /kcomp/= integral_kcomp//= integral_dirac//= diracT mul1e. + rewrite (eq_integral (EFin \o \1_[set inr x | x in V])). + by rewrite integral_indic ?setIT ?unitE. + move=> [x|x] xV /=; rewrite indicE. + rewrite ?inl_in_set_inr /kcomp/=. + by rewrite ge0_integral_mscale//= ?normr0 mul0e. + by rewrite inr_in_set_inr// indicE. +rewrite !mE trE'. +suff -> : t (gamma, tt) (range inr) = 1 - t (gamma, tt) [set inl tt]. + by rewrite tlE -EFinB/= orbF eqe subr_eq0 eq_sym lt_eqF. +rewrite -( @prob_kernel _ _ _ _ _ t (gamma, tt) ). +have -> : [set: unit + B] = [set inl tt] `|` (range inr). + symmetry; apply/eq_set => -[[]|b]//=; apply/propT; first by left. + by right; exists b. +rewrite measureU//=; first by rewrite addeAC subee ?add0e// ?tlE//. +by apply/eq_set => -[[]|b]//=; apply/propF; case=> []// _ []. +Qed. + +End iterate_unit. + (* an s-finite kernel to test that two expressions are different *) Section lift_neq. Context {R : realType} d (G : measurableType d). @@ -1379,17 +1504,15 @@ Definition trick : R.-sfker (T * unit) ~> (unit + bool)%type := (letin (ret macc1of4) (ret minrb)) (ret minltt)))). -Definition von_neumann_trick_kernel : _ -> _ := +Definition kvon_neumann_trick : _ -> _ := (@iterate _ _ R _ unit _ bool trick _ ktt). -Definition von_neumann_trick x : _ -> _ := von_neumann_trick_kernel x. +Definition von_neumann_trick x : _ -> _ := kvon_neumann_trick x. -HB.instance Definition _ := SFiniteKernel.on von_neumann_trick_kernel. +HB.instance Definition _ := SFiniteKernel.on kvon_neumann_trick. HB.instance Definition _ x := Measure.on (von_neumann_trick x). Section von_neumann_trick_proof. -Hypothesis D_nontrivial : 0 < D [set true] < 1. - Let p : R := fine (D [set true]). Let q : R := p * (1 - p). Let r : R := p ^+ 2 + (1 - p) ^+ 2. @@ -1432,6 +1555,30 @@ by case: (set_bool A) => /eqP->/=; -EFinD addrNK. Qed. +Lemma trick_bool gamma b : trick gamma [set inr b] = q%:E. +Proof. +rewrite /trick/= /kcomp /= intDE//= /kcomp/= !intDE//= /kcomp. +rewrite !integral_dirac ?diracT//= ?mul1e. +rewrite !iteE//= ?diracE/= /kcomp/=. +rewrite !integral_dirac ?diracT ?diracE ?mul1e//=. +rewrite /finrb ?inl_in_set_inr//= /acc1of4/= ?inr_in_set_inr !in_set1 !inE. +rewrite /q -?(EFinB, EFinN, EFinM, EFinD); congr (_)%:E. +by case: b => //=; ring. +Qed. + +Lemma trick_unit gamma : trick gamma [set inl tt] = r%:E. +Proof. +rewrite /trick/= /kcomp /= intDE//= /kcomp/= !intDE//= /kcomp. +rewrite !integral_dirac ?diracT//= ?mul1e. +rewrite !iteE//= ?diracE/= /kcomp/=. +rewrite !integral_dirac ?diracT ?diracE ?mul1e//=. +rewrite /finrb ?inl_in_set_inr//= /acc1of4/= ?inr_in_set_inr. +rewrite !in_set1 !inE/=. +by rewrite /r -?(EFinB, EFinN, EFinM, EFinD); congr (_)%:E; ring. +Qed. + +Hypothesis D_nontrivial : 0 < D [set true] < 1. + Let p_gt0 : (0 < p)%R. Proof. by rewrite -lte_fin -Dtrue; case/andP : D_nontrivial. Qed. @@ -1446,74 +1593,23 @@ rewrite /r -subr_gt0 [ltRHS](_ : _ = 2 * p * (1 - p))%R; last by ring. by rewrite !mulr_gt0. Qed. -Lemma iterate_trick gamma n (b : bool) : - @iterate_ _ _ R _ unit _ bool trick _ ktt n gamma [set b] - = (geometric q r n)%:E. -Proof. -elim: n => [|n IHn] //=; rewrite /kcomp; rewrite integral_kcomp//=; - rewrite integral_dirac//= ?diracT ?mul1e integral_kcomp//=; - under eq_integral do [rewrite integral_kcomp//=; - under eq_integral do rewrite integral_kcomp//= - ?integral_dirac//= ?diracT ?mul1e]; - do ![rewrite intDE//=; last by - [move=> b'; do !rewrite integral_ge0//= => *]]; - rewrite !iteE//= ?integral_kcomp//= - ?integral_dirac//= ?diracT ?mul1e/=; - rewrite /acc1of4/= /kcomp ?ge0_integral_mscale //= - ?diracE/= ?in_set1 ?inE/= - ?(mule0, mul0e, adde0, add0e, mule1, normr0)//=; - rewrite -?(EFinB, EFinN, EFinM, EFinD) ?lee_fin ?expr0 ?mulr1//. - by case: b => //=; rewrite ?(mulr0, mulr1, add0r, addr0)// ?[(_ * p)%R]mulrC. -rewrite IHn -?(EFinB, EFinN, EFinM, EFinD) ?lee_fin ?expr0 ?mulr1//. -rewrite [geometric _ _ _]lock !mulrA -mulrDl addrC. -by rewrite /geometric/= -/r -lock mulrCA exprS. -Qed. - -Lemma iterate_trickT gamma n : - @iterate_ _ _ R _ unit _ bool trick _ ktt n gamma [set: bool] = - (2 * geometric q r n)%:E. -Proof. -rewrite setT_bool measureU//=; last first. - by rewrite disjoints_subset => -[]//. -by rewrite !iterate_trick -EFinD -mulr2n mulr_natl. -Qed. - Lemma von_neumann_trick_prob_kernel gamma b : - von_neumann_trick_kernel gamma [set b] = 2^-1%:E. + kvon_neumann_trick gamma [set b] = 2^-1%:E. Proof. -rewrite /= /kcomp/= /case_nat_/= /mseries. -under eq_integral => n _. - under (@congr_lim _ _ _ \o @eq_fun _ _ _ _) => k. - under eq_bigr do rewrite fun_if/= (fun_if (@^~ _))/mzero eq_sym. - rewrite -big_mkcond/= big_nat1_eq iterate_trick. - over. -over. -rewrite /= (eq_integral (EFin \o geometric q r))//=; last first. - by move=> k _; apply/lim_near_cst => //; near do rewrite ifT//. -have cvgg: series (geometric q r) x @[x --> \oo] --> (q / (1 - r))%R. - by apply/cvg_geometric_series; rewrite ger0_norm ?r_lt1. -have limgg := cvg_lim (@Rhausdorff R) cvgg. -have sumgE : \big[+%R/0%R]_(0 <= k *; field. rewrite [X in X != _](_ : _ = 2 * (p * (1 - p)))%R; last by ring. - by rewrite mulf_eq0 ?pnatr_eq0/= mulf_neq0// gt_eqF ?p_gt0 ?p'_gt0//. -suff summableg : summable setT (EFin \o geometric q r) - by rewrite integral_count. -rewrite /summable /=. -rewrite (_ : [set: nat] = [set x | x \in predT]); last first. - by apply/eq_set => x; rewrite inE trueE. -rewrite -(@nneseries_esum _ _ predT)//=. -under eq_eseriesr do rewrite ger0_norm// ?geometric_ge0//. -by rewrite sumgE ltey. -Unshelve. all: end_near. Qed. + by rewrite mulf_eq0 ?pnatr_eq0/= mulf_neq0// gt_eqF ?p_gt0 ?p'_gt0. +- by move=> gamma'; exact: trick_unit. +- suff -> : [set inr x | x in [set b]] = [set inr b] by exact: trick_bool. + by move=> A; apply/seteqP; split=> [a [_ ->]|_ ->]//=; exists b. +Qed. Lemma von_neumann_trick_prob_kernelT gamma : von_neumann_trick gamma [set: bool] = 1. Proof. -rewrite setT_bool measureU//=; last first. - by rewrite disjoints_subset => -[]. +rewrite setT_bool measureU//=; last by rewrite disjoints_subset => -[]. rewrite !von_neumann_trick_prob_kernel -EFinD. by have := splitr (1 : R); rewrite mul1r => <-. Qed. @@ -1522,7 +1618,7 @@ HB.instance Definition _ gamma := Measure.on (von_neumann_trick gamma). HB.instance Definition _ gamma := Measure_isProbability.Build _ _ _ (von_neumann_trick gamma) (von_neumann_trick_prob_kernelT gamma). HB.instance Definition _ := Kernel_isProbability.Build _ _ _ _ _ - von_neumann_trick_kernel von_neumann_trick_prob_kernelT. + kvon_neumann_trick von_neumann_trick_prob_kernelT. Theorem von_neumann_trickP gamma : von_neumann_trick gamma =1 bernoulli 2^-1. Proof. by apply: eq_bernoulli; rewrite von_neumann_trick_prob_kernel. Qed. From 464250955c1e5de72ae5daa54aaf396759fc187c Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Wed, 1 Jan 2025 17:52:33 +0900 Subject: [PATCH 34/36] doc --- reals/constructive_ereal.v | 5 + theories/prob_lang.v | 485 +++++++++++++++++-------------------- theories/prob_lang_wip.v | 10 +- 3 files changed, 233 insertions(+), 267 deletions(-) diff --git a/reals/constructive_ereal.v b/reals/constructive_ereal.v index 348fa231f..b94becb26 100644 --- a/reals/constructive_ereal.v +++ b/reals/constructive_ereal.v @@ -2407,6 +2407,11 @@ Qed. Lemma EFin_max : {morph (@EFin R) : r s / Num.max r s >-> maxe r s}. Proof. by move=> a b /=; rewrite -fine_max. Qed. +Lemma EFin_bigmax {I : Type} (s : seq I) (P : I -> bool) (F : I -> R) r : + \big[maxe/r%:E]_(i <- s | P i) (F i)%:E = + (\big[Num.max/r]_(i <- s | P i) F i)%:E. +Proof. by rewrite (big_morph _ EFin_max erefl). Qed. + Lemma fine_min : {in fin_num &, {mono @fine R : x y / mine x y >-> (Num.min x y)%:E}}. Proof. diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 70b89df55..9112384b6 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -16,26 +16,62 @@ From mathcomp Require Import ring lra. (* Reference: *) (* - R. Affeldt, C. Cohen, A. Saito. Semantics of probabilistic programs *) (* using s-finite kernels in Coq. CPP 2023 *) +(* - S. Staton. Commutative Semantics for Probabilistic Programming. *) +(* ESOP 2017 *) (* *) (* ``` *) (* poisson_pdf == Poisson pdf *) (* exponential_pdf == exponential distribution pdf *) +(* measurable_sum X Y == the type X + Y, as a measurable type *) +(* *) +(* mscore f t := mscale `|f t| \d_tt *) +(* kscore f := fun=> mscore f *) +(* This is an s-finite kernel. *) +(* kite k1 k2 mf := kdirac mf \; kadd (kiteT k1) (kiteF k2). *) +(* k1 has type R.-sfker T ~> T'. *) +(* k2 has type R.-sfker T ~> T'. *) +(* mf is a proof that f : T -> bool is measurable. *) +(* KITE.kiteT k1 is k1 \o fst if f returne true *) +(* and zero otherwise. *) +(* KITE.kiteF k2 is k2 \o fst if f returne false *) +(* and zero otherwise. *) (* *) -(* sample mP == sample according to the probability P where mP is *) -(* a proof that P is a measurable function *) -(* sample_cst P == sample according to the probability P *) -(* letin l k == execute l, augment the context, and execute k *) (* ret mf == access the context with f and return the result *) -(* score mf == observe t from d, where f is the density of d and *) -(* t occurs in f *) -(* e.g., score (r e^(-r * t)) = observe t from exp(r) *) -(* normalize k P == normalize the kernel k into a probability kernel, *) +(* mf is a proof that f is measurable. *) +(* This is a probability kernel. *) +(* sample mP == sample according to the probability measure P *) +(* mP is a proof that P is a measurable function. *) +(* sample_cst P == same as sample with a constant probability measure *) +(* normalize k P == normalize the kernel k into a probability kernel *) (* P is a default probability in case normalization *) -(* is not possible *) +(* is not possible. *) +(* normalize_pt k := normalize k point *) (* ite mf k1 k2 == access the context with the boolean function f and *) (* behaves as k1 or k2 according to the result *) -(* case_nat == case analysis on the nat datatype *) +(* letin l k == execute l, augment the context, and execute k *) +(* fail := let _ := score 0 in ret point *) +(* score mf == observe t from d, where f is the density of d and *) +(* t occurs in f *) +(* e.g., score (r e^(-r * t)) = observe t from exp(r) *) +(* acc0of2, acc1of2, etc. == accessor function *) +(* case_nat t u_ == case analysis on natural numbers *) +(* t has type R.-sfker T ~> nat *) +(* u_ has type nat -> R.-sfker T ~> T' *) +(* CASE_SUM.case_sum g k1 k2 == case analysis on a sum type *) +(* g has type R.-sfker X ~> (A + B). *) +(* k1 has type A -> R.-sfker X ~> Y. *) +(* k2 has type B -> R.-sfker X ~> Y. *) +(* kcounting == the counting measure as a kernel *) +(* iterate k mu == iteration *) +(* k has type R.-sfker G * A ~> (A + B). *) +(* mu is a proof that u : G -> A is measurable. *) +(* flift_neq == an s-finite kernel to test that two expressions *) +(* are different *) +(* ``` *) +(* *) +(* Examples: Staton's bus, von Neumann's trick, etc. *) (* *) +(* ``` *) (* mkswap k == given a kernel k : (Y * X) ~> Z, *) (* returns a kernel of type (X * Y) ~> Z *) (* letin' := mkcomp \o mkswap *) @@ -77,14 +113,12 @@ subst p2. by f_equal. Qed. -Definition dep_uncurry (A : Type) (B : A -> Type) (C : Type) : - (forall a : A, B a -> C) -> {a : A & B a} -> C := - fun f p => let (a, Ba) := p in f a Ba. - +(* NB: to be PRed to probability.v *) Section poisson_pdf. Variable R : realType. Local Open Scope ring_scope. +(* density function for Poisson *) Definition poisson_pdf k r : R := if r > 0 then r ^+ k / k`!%:R^-1 * expR (- r) else 1%:R. @@ -108,12 +142,16 @@ by apply: measurable_funM => /=; [exact: measurable_funM|exact: measurableT_comp]. Qed. +Definition poisson3 := poisson_pdf 4 3%:R. (* 0.168 *) +Definition poisson10 := poisson_pdf 4 10%:R. (* 0.019 *) + End poisson_pdf. Section exponential_pdf. Variable R : realType. Local Open Scope ring_scope. +(* density function for exponential *) Definition exponential_pdf x r : R := r * expR (- r * x). Lemma exponential_pdf_gt0 x r : 0 < r -> 0 < exponential_pdf x r. @@ -147,8 +185,8 @@ Let sumU (F : (set (X + Y))^nat) : (forall i, measurable_sum (F i)) -> measurable_sum (\bigcup_i F i). Proof. by []. Qed. -HB.instance Definition _ := @isMeasurable.Build default_measure_display (X + Y)%type - measurable_sum sum0 sumC sumU. +HB.instance Definition _ := @isMeasurable.Build default_measure_display + (X + Y)%type measurable_sum sum0 sumC sumU. End measurable_sum. @@ -179,11 +217,6 @@ move=> mx my; apply: measurable_fun_ifT => //=. - exact: measurableT_comp. Qed. -(* Definition mR (R : realType) : Type := R. -HB.instance Definition _ (R : realType) := Measurable.on (mR R). -(* [the measurableType (R.-ocitv.-measurable).-sigma of - salgebraType (R.-ocitv.-measurable)]. *) *) - Module Notations. Notation munit := Datatypes_unit__canonical__measure_Measurable. Notation mbool := Datatypes_bool__canonical__measure_Measurable. @@ -194,6 +227,7 @@ Lemma invr_nonneg_proof (R : numDomainType) (p : {nonneg R}) : (0 <= (p%:num)^-1)%R. Proof. by rewrite invr_ge0. Qed. +(* TODO: move *) Definition invr_nonneg (R : numDomainType) (p : {nonneg R}) := NngNum (invr_nonneg_proof p). @@ -249,7 +283,7 @@ Qed. End mscore. -(* decomposition of score into finite kernels *) +(* decomposition of score into finite kernels [Section 3.2, Staton ESOP 2017] *) Module SCORE. Section score. Context d (T : measurableType d) (R : realType). @@ -389,7 +423,7 @@ HB.instance Definition _ := End kscore. -(* decomposition of ite into s-finite kernels *) +(* decomposition of ite into s-finite kernels [Section 3.2, Staton ESOP 2017] *) Module ITE. Section ite. Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). @@ -536,10 +570,6 @@ HB.instance Definition _ t := isMeasure.Build _ _ _ (mite mf t) Import ITE. -(* -Definition kite : R.-sfker T ~> T' := - kdirac mf \; kadd (kiteT u1) (kiteF u2). -*) Definition kite : R.-sfker T ~> T' := kdirac mf \; kadd (kiteT u1) (kiteF u2). @@ -631,7 +661,7 @@ Section insn3. Context d d' d3 (X : measurableType d) (Y : measurableType d') (Z : measurableType d3) (R : realType). -Definition letin (l : R.-sfker X ~> Y) (k : R.-sfker [the measurableType _ of (X * Y)%type] ~> Z) +Definition letin (l : R.-sfker X ~> Y) (k : R.-sfker (X * Y) ~> Z) : R.-sfker X ~> Z := [the R.-sfker X ~> Z of l \; k]. @@ -677,8 +707,7 @@ End letin_return. Section insn1. Context d (X : measurableType d) (R : realType). -Definition score (f : X -> R) (mf : measurable_fun setT f) - : R.-sfker X ~> _ := +Definition score (f : X -> R) (mf : measurable_fun setT f) : R.-sfker X ~> _ := [the R.-sfker X ~> _ of kscore mf]. End insn1. @@ -704,7 +733,6 @@ Definition k3 : measurable_fun _ _ := kr 3%:R. Definition k10 : measurable_fun _ _ := kr 10%:R. Definition ktt := @measurable_cst _ _ T _ setT tt. Definition kb (b : bool) := @measurable_cst _ _ T _ setT b. - Definition kn (n : nat) := @measurable_cst _ _ T _ setT n. End cst_fun. @@ -837,7 +865,7 @@ Import Notations. Context d0 d1 d2 d3 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) (T3 : measurableType d3) (R : realType). -Definition T01 : seq {d & measurableType d} := [:: existT _ _ T0; existT _ _ T1]. +Let T01 : seq {d & measurableType d} := [:: existT _ _ T0; existT _ _ T1]. Definition acc0of2 : T0 * T1 -> T0 := acc T01 0 \o pairAr unit tt. @@ -855,7 +883,7 @@ Proof. by apply: measurableT_comp; [exact: (measurable_acc T01 1)|exact: mpairAr]. Qed. -Definition T02 := [:: existT _ _ T0; existT _ _ T1; existT _ _ T2]. +Let T02 := [:: existT _ _ T0; existT _ _ T1; existT _ _ T2]. Definition acc1of3 : T0 * T1 * T2 -> T1 := acc T02 1 \o pairAAr. @@ -897,8 +925,7 @@ Proof. by apply: measurableT_comp; [exact: (measurable_acc T02 2)|exact: mpairAArAi]. Qed. -Definition T03 := - [:: existT _ _ T0; existT _ _ T1; existT _ d2 T2; existT _ d3 T3]. +Let T03 := [:: existT _ _ T0; existT _ _ T1; existT _ d2 T2; existT _ d3 T3]. Definition acc1of4 : T0 * T1 * T2 * T3 -> T1 := acc T03 1 \o pairAAAr. @@ -936,61 +963,6 @@ Arguments macc1of4 {d0 d1 d2 d3 _ _ _ _}. Arguments macc2of4' {d0 d1 d2 d3 _ _ _ _}. Arguments macc3of4 {d0 d1 d2 d3 _ _ _ _}. -(* sample programs *) -Section poisson. -Variable R : realType. -Local Open Scope ring_scope. - -(* density function for Poisson *) -Definition poisson k r : R := - if r > 0 then r ^+ k / k`!%:R^-1 * expR (- r) else 1%:R. - -Lemma poisson_ge0 k r : 0 <= poisson k r. -Proof. -rewrite /poisson; case: ifPn => r0//. -by rewrite mulr_ge0 ?expR_ge0// mulr_ge0// exprn_ge0 ?ltW. -Qed. - -Lemma poisson_gt0 k r : 0 < r -> 0 < poisson k.+1 r. -Proof. -move=> r0; rewrite /poisson r0 mulr_gt0 ?expR_gt0//. -by rewrite divr_gt0// ?exprn_gt0// invr_gt0 ltr0n fact_gt0. -Qed. - -Lemma measurable_poisson k : measurable_fun setT (poisson k). -Proof. -rewrite /poisson; apply: measurable_fun_if => //. - exact: measurable_fun_ltr. -by apply: measurable_funM => /=; - [exact: measurable_funM|exact: measurableT_comp]. -Qed. - -Definition poisson3 := poisson 4 3%:R. (* 0.168 *) -Definition poisson10 := poisson 4 10%:R. (* 0.019 *) - -End poisson. - -Section exponential. -Variable R : realType. -Local Open Scope ring_scope. - -(* density function for exponential *) -Definition exp_density x r : R := r * expR (- r * x). - -Lemma exp_density_gt0 x r : 0 < r -> 0 < exp_density x r. -Proof. by move=> r0; rewrite /exp_density mulr_gt0// expR_gt0. Qed. - -Lemma exp_density_ge0 x r : 0 <= r -> 0 <= exp_density x r. -Proof. by move=> r0; rewrite /exp_density mulr_ge0// expR_ge0. Qed. - -Lemma mexp_density x : measurable_fun setT (exp_density x). -Proof. -apply: measurable_funM => //=; apply: measurableT_comp => //. -exact: measurable_funM. -Qed. - -End exponential. - Module CASE_NAT. Section case_nat. Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). @@ -1071,10 +1043,10 @@ Definition case_nat (t : R.-sfker T ~> nat) (u_ : (R.-sfker T ~> T')^nat) End case_nat. Definition measure_sum_display : - (measure_display * measure_display) -> measure_display. + measure_display * measure_display -> measure_display. Proof. exact. Qed. -Definition image_classes d1 d2 +Definition g_sigma_imageU d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) (T : Type) (f1 : T1 -> T) (f2 : T2 -> T) := < T1 + T2 := @inl T1 T2. Let f2 : T2 -> T1 + T2 := @inr T1 T2. -Lemma sum_salgebra_set0 : image_classes f1 f2 (set0 : set (T1 + T2)). +Lemma sum_salgebra_set0 : g_sigma_imageU f1 f2 (set0 : set (T1 + T2)). Proof. exact: sigma_algebra0. Qed. -Lemma sum_salgebra_setC A : image_classes f1 f2 A -> - image_classes f1 f2 (~` A). +Lemma sum_salgebra_setC A : g_sigma_imageU f1 f2 A -> + g_sigma_imageU f1 f2 (~` A). Proof. exact: sigma_algebraC. Qed. -Lemma sum_salgebra_bigcup (F : _^nat) : (forall i, image_classes f1 f2 (F i)) -> - image_classes f1 f2 (\bigcup_i (F i)). +Lemma sum_salgebra_bigcup (F : _^nat) : (forall i, g_sigma_imageU f1 f2 (F i)) -> + g_sigma_imageU f1 f2 (\bigcup_i (F i)). Proof. exact: sigma_algebra_bigcup. Qed. HB.instance Definition sum_salgebra_mixin := @isMeasurable.Build (measure_sum_display (d1, d2)) - (T1 + T2)%type (image_classes f1 f2) + (T1 + T2)%type (g_sigma_imageU f1 f2) sum_salgebra_set0 sum_salgebra_setC sum_salgebra_bigcup. End sum_salgebra_instance. @@ -1110,13 +1082,6 @@ Notation "p .-sum.-measurable" := ((p.-sum).-measurable : set (set (_ + _))) : classical_set_scope. -(* TODO: move *) -Lemma bigmaxEFin {R : realDomainType} {I : Type} (s : seq I) (P : I -> bool) - (F : I -> R) (x : R) : - (\big[Order.max/x%:E]_(i <- s | P i) (F i)%:E)%R = - (\big[Order.max/x]_(i <- s | P i) F i)%:E. -Proof. by rewrite (big_morph _ EFin_max erefl). Qed. - #[short(type="measurableCountType")] HB.structure Definition MeasurableCountable d := {T of Measurable d T & Countable T }. @@ -1233,14 +1198,14 @@ exists (fun n => case_sum' (f1 ^~ n) (f2 ^~ n)). move=> /= [x [a|b]]. - have [bnd Hbnd] := measure_uub (f1 a n). rewrite EFin_max lt_max; apply/orP; left. - rewrite /case_sum' -bigmaxEFin. + rewrite /case_sum' -EFin_bigmax. apply: lt_le_trans; last exact: le_bigmax_cond. by rewrite /f1'; case: cid => /=. - have [bnd Hbnd] := measure_uub (f2 b n). rewrite EFin_max lt_max; apply/orP; right. - rewrite /case_sum' -bigmaxEFin. + rewrite /case_sum' -EFin_bigmax. apply: lt_le_trans; last exact: le_bigmax_cond. - by rewrite /f2'; case: cid => /=. + by rewrite /f2'; case: cid => /=C. move=> [x [a|b]] U mU/=-. - by rewrite (Hf1 a x _ mU). - by rewrite (Hf2 b x _ mU). @@ -1291,7 +1256,7 @@ HB.instance Definition _ := End kcounting. -(* formalization of the iterate construct of Staton ESOP 2017, Sect. 4.2 *) +(* formalization of the iterate construct [Section 4.2, Staton ESOP 2017] *) Section iterate. Context d {G : measurableType d} {R : realType}. Context dA (A : measurableFinType dA) dB (B : measurableFinType dB). @@ -1446,7 +1411,6 @@ Qed. End iterate_unit. -(* an s-finite kernel to test that two expressions are different *) Section lift_neq. Context {R : realType} d (G : measurableType d). Variables (f : G -> bool) (g : G -> bool). @@ -1478,155 +1442,6 @@ Definition lift_neq : R.-sfker G ~> bool := ret measurable_fun_flift_neq. End lift_neq. -Section von_neumann_trick. -Context d {T : measurableType d} {R : realType}. - -Definition minltt {d1 d2} {T1 : measurableType d1} {T2 : measurableType d2} := - @measurable_cst _ _ T1 _ setT (@inl _ T2 tt). - -Definition finrb d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) : - T1 * bool -> T2 + bool := fun t1b => inr t1b.2. - -Lemma minrb {d1 d2} {T1 : measurableType d1} {T2 : measurableType d2} : - measurable_fun setT (@finrb _ _ T1 T2). -Proof. exact: measurableT_comp. Qed. - -(* biased coin *) -Variable (D : pprobability bool R). -Let unit := measurableTypeUnit. -Let bool := measurableTypeBool. - -Definition trick : R.-sfker (T * unit) ~> (unit + bool)%type := - letin (sample_cst D) - (letin (sample_cst D) - (letin (lift_neq macc1of3 macc2of3) - (ite macc3of4 - (letin (ret macc1of4) (ret minrb)) - (ret minltt)))). - -Definition kvon_neumann_trick : _ -> _ := - (@iterate _ _ R _ unit _ bool trick _ ktt). -Definition von_neumann_trick x : _ -> _ := kvon_neumann_trick x. - -HB.instance Definition _ := SFiniteKernel.on kvon_neumann_trick. -HB.instance Definition _ x := Measure.on (von_neumann_trick x). - -Section von_neumann_trick_proof. - -Let p : R := fine (D [set true]). -Let q : R := p * (1 - p). -Let r : R := p ^+ 2 + (1 - p) ^+ 2. - -Let Dtrue : D [set true] = p%:E. -Proof. by rewrite fineK//= fin_num_measure. Qed. - -Let Dfalse : D [set false] = 1 - (p%:E). -Proof. -rewrite -Dtrue; have <- : D [set: bool] = 1 := probability_setT. -rewrite setT_bool measureU//=; last first. - by rewrite disjoints_subset => -[]//. -by rewrite addeAC subee ?fin_num_measure ?add0e. -Qed. - -Let p_ge0 : (0 <= p)%R. -Proof. by rewrite fine_ge0 ?measure_ge0. Qed. - -Let p'_ge0 : (1 - p >= 0)%R. -Proof. by rewrite -lee_fin EFinB -Dfalse measure_ge0. Qed. - -Let q_ge0 : (q >= 0)%R. Proof. by rewrite mulr_ge0 ?p_ge0 ?p'_ge0. Qed. - -Let r_ge0 : (r >= 0)%R. -Proof. by rewrite addr_ge0// exprn_ge0 ?p_ge0 ?p'_ge0. Qed. - -Let intDE f : (forall x, 0 <= f x) -> - \int[D]_x (f x) = (1 - (p%:E)) * f false + p%:E * f true. -Proof. -move=> f_ge0. -pose M := measure_add (mscale (NngNum p'_ge0) \d_false) - (mscale (NngNum p_ge0) \d_true). -rewrite (eq_measure_integral M) => [|A Ameas _]. - rewrite ge0_integral_measure_add//. - by rewrite !ge0_integral_mscale ?integral_dirac ?diracT ?mul1e. -rewrite /M/= /msum/= !big_ord_recl/= big_ord0 addr0 /mscale/=. -by case: (set_bool A) => /eqP->/=; - rewrite ?measure0 ?probability_setT ?Dtrue ?Dfalse//= - ?diracE/= ?in_set1 ?inE/= ?(mule0, mul0e, adde0, add0e, mule1)// - -EFinD addrNK. -Qed. - -Lemma trick_bool gamma b : trick gamma [set inr b] = q%:E. -Proof. -rewrite /trick/= /kcomp /= intDE//= /kcomp/= !intDE//= /kcomp. -rewrite !integral_dirac ?diracT//= ?mul1e. -rewrite !iteE//= ?diracE/= /kcomp/=. -rewrite !integral_dirac ?diracT ?diracE ?mul1e//=. -rewrite /finrb ?inl_in_set_inr//= /acc1of4/= ?inr_in_set_inr !in_set1 !inE. -rewrite /q -?(EFinB, EFinN, EFinM, EFinD); congr (_)%:E. -by case: b => //=; ring. -Qed. - -Lemma trick_unit gamma : trick gamma [set inl tt] = r%:E. -Proof. -rewrite /trick/= /kcomp /= intDE//= /kcomp/= !intDE//= /kcomp. -rewrite !integral_dirac ?diracT//= ?mul1e. -rewrite !iteE//= ?diracE/= /kcomp/=. -rewrite !integral_dirac ?diracT ?diracE ?mul1e//=. -rewrite /finrb ?inl_in_set_inr//= /acc1of4/= ?inr_in_set_inr. -rewrite !in_set1 !inE/=. -by rewrite /r -?(EFinB, EFinN, EFinM, EFinD); congr (_)%:E; ring. -Qed. - -Hypothesis D_nontrivial : 0 < D [set true] < 1. - -Let p_gt0 : (0 < p)%R. -Proof. by rewrite -lte_fin -Dtrue; case/andP : D_nontrivial. Qed. - -Let p_lt1 : (p < 1)%R. -Proof. by rewrite -lte_fin -Dtrue; case/andP : D_nontrivial. Qed. - -Let p'_gt0 : (0 < 1 - p)%R. Proof. by rewrite subr_gt0. Qed. - -Let r_lt1 : (r < 1)%R. -Proof. -rewrite /r -subr_gt0 [ltRHS](_ : _ = 2 * p * (1 - p))%R; last by ring. -by rewrite !mulr_gt0. -Qed. - -Lemma von_neumann_trick_prob_kernel gamma b : - kvon_neumann_trick gamma [set b] = 2^-1%:E. -Proof. -rewrite [LHS](@iterateE _ _ _ _ _ _ _ _ r _ _ _ q)//=. -- rewrite /r /q; congr (_)%:E. - suff: (1 - ((p ^+ 2)%R + ((1 - p) ^+ 2)%R)%E)%R != 0%R by move=> *; field. - rewrite [X in X != _](_ : _ = 2 * (p * (1 - p)))%R; last by ring. - by rewrite mulf_eq0 ?pnatr_eq0/= mulf_neq0// gt_eqF ?p_gt0 ?p'_gt0. -- by move=> gamma'; exact: trick_unit. -- suff -> : [set inr x | x in [set b]] = [set inr b] by exact: trick_bool. - by move=> A; apply/seteqP; split=> [a [_ ->]|_ ->]//=; exists b. -Qed. - -Lemma von_neumann_trick_prob_kernelT gamma : - von_neumann_trick gamma [set: bool] = 1. -Proof. -rewrite setT_bool measureU//=; last by rewrite disjoints_subset => -[]. -rewrite !von_neumann_trick_prob_kernel -EFinD. -by have := splitr (1 : R); rewrite mul1r => <-. -Qed. - -HB.instance Definition _ gamma := Measure.on (von_neumann_trick gamma). -HB.instance Definition _ gamma := Measure_isProbability.Build _ _ _ - (von_neumann_trick gamma) (von_neumann_trick_prob_kernelT gamma). -HB.instance Definition _ := Kernel_isProbability.Build _ _ _ _ _ - kvon_neumann_trick von_neumann_trick_prob_kernelT. - -Theorem von_neumann_trickP gamma : von_neumann_trick gamma =1 bernoulli 2^-1. -Proof. by apply: eq_bernoulli; rewrite von_neumann_trick_prob_kernel. Qed. - -End von_neumann_trick_proof. - -End von_neumann_trick. - Section insn1_lemmas. Import Notations. Context d (T : measurableType d) (R : realType). @@ -1696,6 +1511,7 @@ Qed. End letin_ite. +(* associativity of let [Section 4.2, Staton ESOP 2017] *) Section letinA. Context d d' d1 d2 d3 (X : measurableType d) (Y : measurableType d') (T1 : measurableType d1) (T2 : measurableType d2) (T3 : measurableType d3) @@ -1724,6 +1540,7 @@ Qed. End letinA. +(* commutativity of let [Section 4.2, Staton ESOP 2017] *) Section letinC. Context d d1 d' (X : measurableType d) (Y : measurableType d1) (Z : measurableType d') (R : realType). @@ -1822,7 +1639,6 @@ Context d (T : measurableType d) (R : realType). (* let x = sample (bernoulli (2/7)) in let r = case x of {(1, _) => return (k3()), (2, _) => return (k10())} in return r *) - Definition sample_and_branch : R.-sfker T ~> _ := letin (sample_cst (bernoulli (2 / 7))) (* T -> B *) @@ -1978,6 +1794,155 @@ Qed. End staton_bus_exponential. + +Section von_neumann_trick. +Context d {T : measurableType d} {R : realType}. + +Definition minltt {d1 d2} {T1 : measurableType d1} {T2 : measurableType d2} := + @measurable_cst _ _ T1 _ setT (@inl _ T2 tt). + +Definition finrb d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) : + T1 * bool -> T2 + bool := fun t1b => inr t1b.2. + +Lemma minrb {d1 d2} {T1 : measurableType d1} {T2 : measurableType d2} : + measurable_fun setT (@finrb _ _ T1 T2). +Proof. exact: measurableT_comp. Qed. + +Variable (D : pprobability bool R). (* biased coin *) +Let unit := measurableTypeUnit. +Let bool := measurableTypeBool. + +Definition trick : R.-sfker (T * unit) ~> (unit + bool)%type := + letin (sample_cst D) + (letin (sample_cst D) + (letin (lift_neq macc1of3 macc2of3) + (ite macc3of4 + (letin (ret macc1of4) (ret minrb)) + (ret minltt)))). + +Definition kvon_neumann_trick : _ -> _ := + (@iterate _ _ R _ unit _ bool trick _ ktt). +Definition von_neumann_trick x : _ -> _ := kvon_neumann_trick x. + +HB.instance Definition _ := SFiniteKernel.on kvon_neumann_trick. +HB.instance Definition _ x := Measure.on (von_neumann_trick x). + +Section von_neumann_trick_proof. + +Let p : R := fine (D [set true]). +Let q : R := p * (1 - p). +Let r : R := p ^+ 2 + (1 - p) ^+ 2. + +Let Dtrue : D [set true] = p%:E. +Proof. by rewrite fineK//= fin_num_measure. Qed. + +Let Dfalse : D [set false] = 1 - (p%:E). +Proof. +rewrite -Dtrue; have <- : D [set: bool] = 1 := probability_setT. +rewrite setT_bool measureU//=; last first. + by rewrite disjoints_subset => -[]//. +by rewrite addeAC subee ?fin_num_measure ?add0e. +Qed. + +Let p_ge0 : (0 <= p)%R. +Proof. by rewrite fine_ge0 ?measure_ge0. Qed. + +Let p'_ge0 : (1 - p >= 0)%R. +Proof. by rewrite -lee_fin EFinB -Dfalse measure_ge0. Qed. + +Let q_ge0 : (q >= 0)%R. Proof. by rewrite mulr_ge0 ?p_ge0 ?p'_ge0. Qed. + +Let r_ge0 : (r >= 0)%R. +Proof. by rewrite addr_ge0// exprn_ge0 ?p_ge0 ?p'_ge0. Qed. + +Let intDE f : (forall x, 0 <= f x) -> + \int[D]_x (f x) = (1 - (p%:E)) * f false + p%:E * f true. +Proof. +move=> f_ge0. +pose M := measure_add (mscale (NngNum p'_ge0) \d_false) + (mscale (NngNum p_ge0) \d_true). +rewrite (eq_measure_integral M) => [|A Ameas _]. + rewrite ge0_integral_measure_add//. + by rewrite !ge0_integral_mscale ?integral_dirac ?diracT ?mul1e. +rewrite /M/= /msum/= !big_ord_recl/= big_ord0 addr0 /mscale/=. +by case: (set_bool A) => /eqP->/=; + rewrite ?measure0 ?probability_setT ?Dtrue ?Dfalse//= + ?diracE/= ?in_set1 ?inE/= ?(mule0, mul0e, adde0, add0e, mule1)// + -EFinD addrNK. +Qed. + +Lemma trick_bool gamma b : trick gamma [set inr b] = q%:E. +Proof. +rewrite /trick/= /kcomp /= intDE//= /kcomp/= !intDE//= /kcomp. +rewrite !integral_dirac ?diracT//= ?mul1e. +rewrite !iteE//= ?diracE/= /kcomp/=. +rewrite !integral_dirac ?diracT ?diracE ?mul1e//=. +rewrite /finrb ?inl_in_set_inr//= /acc1of4/= ?inr_in_set_inr !in_set1 !inE. +rewrite /q -?(EFinB, EFinN, EFinM, EFinD); congr (_)%:E. +by case: b => //=; ring. +Qed. + +Lemma trick_unit gamma : trick gamma [set inl tt] = r%:E. +Proof. +rewrite /trick/= /kcomp /= intDE//= /kcomp/= !intDE//= /kcomp. +rewrite !integral_dirac ?diracT//= ?mul1e. +rewrite !iteE//= ?diracE/= /kcomp/=. +rewrite !integral_dirac ?diracT ?diracE ?mul1e//=. +rewrite /finrb ?inl_in_set_inr//= /acc1of4/= ?inr_in_set_inr. +rewrite !in_set1 !inE/=. +by rewrite /r -?(EFinB, EFinN, EFinM, EFinD); congr (_)%:E; ring. +Qed. + +Hypothesis D_nontrivial : 0 < D [set true] < 1. + +Let p_gt0 : (0 < p)%R. +Proof. by rewrite -lte_fin -Dtrue; case/andP : D_nontrivial. Qed. + +Let p_lt1 : (p < 1)%R. +Proof. by rewrite -lte_fin -Dtrue; case/andP : D_nontrivial. Qed. + +Let p'_gt0 : (0 < 1 - p)%R. Proof. by rewrite subr_gt0. Qed. + +Let r_lt1 : (r < 1)%R. +Proof. +rewrite /r -subr_gt0 [ltRHS](_ : _ = 2 * p * (1 - p))%R; last by ring. +by rewrite !mulr_gt0. +Qed. + +Lemma von_neumann_trick_prob_kernel gamma b : + kvon_neumann_trick gamma [set b] = 2^-1%:E. +Proof. +rewrite [LHS](@iterateE _ _ _ _ _ _ _ _ r _ _ _ q)//=. +- rewrite /r /q; congr (_)%:E. + suff: (1 - ((p ^+ 2)%R + ((1 - p) ^+ 2)%R)%E)%R != 0%R by move=> *; field. + rewrite [X in X != _](_ : _ = 2 * (p * (1 - p)))%R; last by ring. + by rewrite mulf_eq0 ?pnatr_eq0/= mulf_neq0// gt_eqF ?p_gt0 ?p'_gt0. +- by move=> gamma'; exact: trick_unit. +- suff -> : [set inr x | x in [set b]] = [set inr b] by exact: trick_bool. + by move=> A; apply/seteqP; split=> [a [_ ->]|_ ->]//=; exists b. +Qed. + +Lemma von_neumann_trick_prob_kernelT gamma : + von_neumann_trick gamma [set: bool] = 1. +Proof. +rewrite setT_bool measureU//=; last by rewrite disjoints_subset => -[]. +rewrite !von_neumann_trick_prob_kernel -EFinD. +by have := splitr (1 : R); rewrite mul1r => <-. +Qed. + +HB.instance Definition _ gamma := Measure.on (von_neumann_trick gamma). +HB.instance Definition _ gamma := Measure_isProbability.Build _ _ _ + (von_neumann_trick gamma) (von_neumann_trick_prob_kernelT gamma). +HB.instance Definition _ := Kernel_isProbability.Build _ _ _ _ _ + kvon_neumann_trick von_neumann_trick_prob_kernelT. + +Theorem von_neumann_trickP gamma : von_neumann_trick gamma =1 bernoulli 2^-1. +Proof. by apply: eq_bernoulli; rewrite von_neumann_trick_prob_kernel. Qed. + +End von_neumann_trick_proof. + +End von_neumann_trick. + (**md letin' variants *) diff --git a/theories/prob_lang_wip.v b/theories/prob_lang_wip.v index 682b13ea6..e36d17297 100644 --- a/theories/prob_lang_wip.v +++ b/theories/prob_lang_wip.v @@ -269,12 +269,12 @@ move=> /= F mF tF mUF. rewrite /mpoisson1/= integral_bigcup//=; last first. apply/integrableP; split. apply/measurable_EFinP. - exact: measurable_funS (measurable_poisson _). + exact: measurable_funS (measurable_poisson_pdf _). rewrite (_ : (fun x => _) = (EFin \o poisson1)); last first. by apply/funext => x; rewrite gee0_abs// lee_fin poisson1_ge0//. apply: le_lt_trans. apply: (@ge0_subset_integral _ _ _ _ _ setT) => //=. - by apply/measurable_EFinP; exact: measurable_poisson. + by apply/measurable_EFinP; exact: measurable_poisson_pdf. by move=> ? _; rewrite lee_fin poisson1_ge0//. by rewrite /= integral_poisson_density// ltry. apply: is_cvg_ereal_nneg_natsum_cond => n _ _. @@ -285,11 +285,7 @@ HB.instance Definition _ := isMeasure.Build _ _ _ mpoisson1 mpoisson10 mpoisson1_ge0 mpoisson1_sigma_additive. Let mpoisson1_setT : mpoisson1 [set: _] = 1%E. -Proof. -rewrite /mpoisson1. -rewrite /poisson1. -by rewrite integral_poisson_density. -Qed. +Proof. exact: integral_poisson_density. Qed. HB.instance Definition _ := @Measure_isProbability.Build _ _ R mpoisson1 mpoisson1_setT. From c48e72fb02175da0a2588ad67ae9f613c3e4e961 Mon Sep 17 00:00:00 2001 From: Cyril Cohen Date: Thu, 2 Jan 2025 19:16:03 +0100 Subject: [PATCH 35/36] golfing --- theories/prob_lang.v | 430 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 378 insertions(+), 52 deletions(-) diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 9112384b6..24ae6a524 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -1820,6 +1820,9 @@ Definition trick : R.-sfker (T * unit) ~> (unit + bool)%type := (letin (ret macc1of4) (ret minrb)) (ret minltt)))). +HB.instance Definition _ := SFiniteKernel.on trick. +HB.instance Definition _ x := Measure.on (trick x). + Definition kvon_neumann_trick : _ -> _ := (@iterate _ _ R _ unit _ bool trick _ ktt). Definition von_neumann_trick x : _ -> _ := kvon_neumann_trick x. @@ -1836,63 +1839,33 @@ Let r : R := p ^+ 2 + (1 - p) ^+ 2. Let Dtrue : D [set true] = p%:E. Proof. by rewrite fineK//= fin_num_measure. Qed. -Let Dfalse : D [set false] = 1 - (p%:E). -Proof. -rewrite -Dtrue; have <- : D [set: bool] = 1 := probability_setT. -rewrite setT_bool measureU//=; last first. - by rewrite disjoints_subset => -[]//. -by rewrite addeAC subee ?fin_num_measure ?add0e. -Qed. - -Let p_ge0 : (0 <= p)%R. -Proof. by rewrite fine_ge0 ?measure_ge0. Qed. - -Let p'_ge0 : (1 - p >= 0)%R. -Proof. by rewrite -lee_fin EFinB -Dfalse measure_ge0. Qed. - -Let q_ge0 : (q >= 0)%R. Proof. by rewrite mulr_ge0 ?p_ge0 ?p'_ge0. Qed. - -Let r_ge0 : (r >= 0)%R. -Proof. by rewrite addr_ge0// exprn_ge0 ?p_ge0 ?p'_ge0. Qed. - -Let intDE f : (forall x, 0 <= f x) -> - \int[D]_x (f x) = (1 - (p%:E)) * f false + p%:E * f true. -Proof. -move=> f_ge0. -pose M := measure_add (mscale (NngNum p'_ge0) \d_false) - (mscale (NngNum p_ge0) \d_true). -rewrite (eq_measure_integral M) => [|A Ameas _]. - rewrite ge0_integral_measure_add//. - by rewrite !ge0_integral_mscale ?integral_dirac ?diracT ?mul1e. -rewrite /M/= /msum/= !big_ord_recl/= big_ord0 addr0 /mscale/=. -by case: (set_bool A) => /eqP->/=; - rewrite ?measure0 ?probability_setT ?Dtrue ?Dfalse//= - ?diracE/= ?in_set1 ?inE/= ?(mule0, mul0e, adde0, add0e, mule1)// - -EFinD addrNK. -Qed. - -Lemma trick_bool gamma b : trick gamma [set inr b] = q%:E. +Lemma trickE gamma X : trick gamma X = + (r *+ (inl tt \in X) + + q *+ ((inr true \in X) + (inr false \in X)))%:E. Proof. -rewrite /trick/= /kcomp /= intDE//= /kcomp/= !intDE//= /kcomp. +have Dbernoulli : D =1 bernoulli p by exact/eq_bernoulli/Dtrue. +have p_itv01 : (0 <= p <= 1)%R. + by rewrite -2!lee_fin -Dtrue?measure_ge0 ?probability_le1. +pose eqbern := eq_measure_integral _ (fun x _ _ => Dbernoulli x). +rewrite /trick/= /kcomp. +do 2?rewrite ?eqbern ?integral_bernoulli//= /kcomp/=. rewrite !integral_dirac ?diracT//= ?mul1e. rewrite !iteE//= ?diracE/= /kcomp/=. -rewrite !integral_dirac ?diracT ?diracE ?mul1e//=. -rewrite /finrb ?inl_in_set_inr//= /acc1of4/= ?inr_in_set_inr !in_set1 !inE. -rewrite /q -?(EFinB, EFinN, EFinM, EFinD); congr (_)%:E. -by case: b => //=; ring. +rewrite !integral_dirac /acc1of4/= ?diracT ?diracE ?mul1e//. +rewrite /finrb /acc1of4/= -?(EFinB, EFinN, EFinM, EFinD) /q /r /onem. +by congr (_)%:E; do 3!move: (_ \in _) => ? /=; ring. Qed. -Lemma trick_unit gamma : trick gamma [set inl tt] = r%:E. +Lemma trick_prob_kernelT gamma : trick gamma setT = 1. Proof. -rewrite /trick/= /kcomp /= intDE//= /kcomp/= !intDE//= /kcomp. -rewrite !integral_dirac ?diracT//= ?mul1e. -rewrite !iteE//= ?diracE/= /kcomp/=. -rewrite !integral_dirac ?diracT ?diracE ?mul1e//=. -rewrite /finrb ?inl_in_set_inr//= /acc1of4/= ?inr_in_set_inr. -rewrite !in_set1 !inE/=. -by rewrite /r -?(EFinB, EFinN, EFinM, EFinD); congr (_)%:E; ring. +by rewrite trickE !mem_setT mulr2n mulr1n /r /q; congr (_)%:E; ring. Qed. +HB.instance Definition _ gamma := Measure_isProbability.Build _ _ _ + (trick gamma) (trick_prob_kernelT gamma). +HB.instance Definition _ := Kernel_isProbability.Build _ _ _ _ _ + trick trick_prob_kernelT. + Hypothesis D_nontrivial : 0 < D [set true] < 1. Let p_gt0 : (0 < p)%R. @@ -1917,9 +1890,9 @@ rewrite [LHS](@iterateE _ _ _ _ _ _ _ _ r _ _ _ q)//=. suff: (1 - ((p ^+ 2)%R + ((1 - p) ^+ 2)%R)%E)%R != 0%R by move=> *; field. rewrite [X in X != _](_ : _ = 2 * (p * (1 - p)))%R; last by ring. by rewrite mulf_eq0 ?pnatr_eq0/= mulf_neq0// gt_eqF ?p_gt0 ?p'_gt0. -- by move=> gamma'; exact: trick_unit. -- suff -> : [set inr x | x in [set b]] = [set inr b] by exact: trick_bool. - by move=> A; apply/seteqP; split=> [a [_ ->]|_ ->]//=; exists b. +- by move=> gamma'; rewrite trickE//= ?in_set1 ?inE//= addr0. +- rewrite trickE/= ?inl_in_set_inr ?inr_in_set_inr// add0r !in_set1 !inE. + by case: b. Qed. Lemma von_neumann_trick_prob_kernelT gamma : @@ -1943,6 +1916,359 @@ End von_neumann_trick_proof. End von_neumann_trick. +Section insn1_lemmas. +Import Notations. +Context d (T : measurableType d) (R : realType). + +Let kcomp_scoreE d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) + (g : R.-sfker [the measurableType _ of (T1 * unit)%type] ~> T2) + f (mf : measurable_fun setT f) r U : + (score mf \; g) r U = `|f r|%:E * g (r, tt) U. +Proof. +rewrite /= /kcomp /kscore /= ge0_integral_mscale//=. +by rewrite integral_dirac// diracT mul1e. +Qed. + +Lemma scoreE d' (T' : measurableType d') (x : T * T') (U : set T') (f : R -> R) + (r : R) (r0 : (0 <= r)%R) + (f0 : (forall r, 0 <= r -> 0 <= f r)%R) (mf : measurable_fun setT f) : + score (measurableT_comp mf (@macc1of2 _ _ _ _)) + (x, r) (curry (snd \o fst) x @^-1` U) = + (f r)%:E * \d_x.2 U. +Proof. +by rewrite /score/= /mscale/= ger0_norm//= f0. +Qed. + +Lemma score_score (f : R -> R) (g : R * unit -> R) + (mf : measurable_fun setT f) + (mg : measurable_fun setT g) : + letin (score mf) (score mg) = + score (measurable_funM mf (measurableT_comp mg (measurable_pair2 tt))). +Proof. +apply/eq_sfkernel => x U. +rewrite {1}/letin; unlock. +by rewrite kcomp_scoreE/= /mscale/= diracE normrM muleA EFinM. +Qed. + +(* hard constraints to express score below 1 *) +Lemma score_fail (r : R) : (0 <= r <= 1)%R -> + score (kr r) = + letin (sample_cst (bernoulli r) : R.-pker T ~> _) + (ite (@macc1of2 _ _ _ _) (ret ktt) fail). +Proof. +move=> /andP[r0 r1]; apply/eq_sfkernel => x U. +rewrite letinE/= /sample; unlock. +by rewrite /mscale/= ger0_norm// integral_bernoulli ?r0//= 2!iteE//= failE mule0 adde0. +Qed. + +End insn1_lemmas. + +Section letin_ite. +Context d d2 d3 (T : measurableType d) (T2 : measurableType d2) + (Z : measurableType d3) (R : realType). +Variables (k1 k2 : R.-sfker T ~> Z) + (u : R.-sfker [the measurableType _ of (T * Z)%type] ~> T2) + (f : T -> bool) (mf : measurable_fun setT f) + (t : T) (U : set T2). + +Lemma letin_iteT : f t -> letin (ite mf k1 k2) u t U = letin k1 u t U. +Proof. +move=> ftT; rewrite !letinE/=; apply: eq_measure_integral => V mV _. +by rewrite iteE ftT. +Qed. + +Lemma letin_iteF : ~~ f t -> letin (ite mf k1 k2) u t U = letin k2 u t U. +Proof. +move=> ftF; rewrite !letinE/=; apply: eq_measure_integral => V mV _. +by rewrite iteE (negbTE ftF). +Qed. + +End letin_ite. + +Section letinA. +Context d d' d1 d2 d3 (X : measurableType d) (Y : measurableType d') + (T1 : measurableType d1) (T2 : measurableType d2) (T3 : measurableType d3) + (R : realType). +Import Notations. +Variables (t : R.-sfker X ~> T1) + (u : R.-sfker (X * T1) ~> T2) + (v : R.-sfker (X * T2) ~> Y) + (v' : R.-sfker (X * T1 * T2) ~> Y) + (vv' : forall y, v =1 fun xz => v' (xz.1, y, xz.2)). + +Lemma letinA x A : measurable A -> + letin t (letin u v') x A + = + (letin (letin t u) v) x A. +Proof. +move=> mA. +rewrite !letinE. +under eq_integral do rewrite letinE. +rewrite integral_kcomp; [|by []|]. +- apply: eq_integral => y _. + apply: eq_integral => z _. + by rewrite (vv' y). +- exact: (measurableT_comp (measurable_kernel v _ mA)). +Qed. + +End letinA. + +Section letinC. +Context d d1 d' (X : measurableType d) (Y : measurableType d1) + (Z : measurableType d') (R : realType). + +Import Notations. + +Variables (t : R.-sfker Z ~> X) + (t' : R.-sfker [the measurableType _ of (Z * Y)%type] ~> X) + (tt' : forall y, t =1 fun z => t' (z, y)) + (u : R.-sfker Z ~> Y) + (u' : R.-sfker [the measurableType _ of (Z * X)%type] ~> Y) + (uu' : forall x, u =1 fun z => u' (z, x)). + +Definition T z : set X -> \bar R := t z. +Let T0 z : (T z) set0 = 0. Proof. by []. Qed. +Let T_ge0 z x : 0 <= (T z) x. Proof. by []. Qed. +Let T_semi_sigma_additive z : semi_sigma_additive (T z). +Proof. exact: measure_semi_sigma_additive. Qed. +HB.instance Definition _ z := @isMeasure.Build _ X R (T z) (T0 z) (T_ge0 z) + (@T_semi_sigma_additive z). + +Let sfinT z : sfinite_measure (T z). Proof. exact: sfinite_kernel_measure. Qed. +HB.instance Definition _ z := @isSFinite.Build _ X R (T z) (sfinT z). + +Definition U z : set Y -> \bar R := u z. +Let U0 z : (U z) set0 = 0. Proof. by []. Qed. +Let U_ge0 z x : 0 <= (U z) x. Proof. by []. Qed. +Let U_semi_sigma_additive z : semi_sigma_additive (U z). +Proof. exact: measure_semi_sigma_additive. Qed. +HB.instance Definition _ z := @isMeasure.Build _ Y R (U z) (U0 z) (U_ge0 z) + (@U_semi_sigma_additive z). + +Let sfinU z : sfinite_measure (U z). Proof. exact: sfinite_kernel_measure. Qed. +HB.instance Definition _ z := @isSFinite.Build _ Y R (U z) (sfinU z). + +Lemma letinC z A : measurable A -> + letin t + (letin u' + (ret (measurable_fun_prod macc1of3 macc2of3))) z A = + letin u + (letin t' + (ret (measurable_fun_prod macc2of3 macc1of3))) z A. +Proof. +move=> mA. +rewrite !letinE. +under eq_integral. + move=> x _. + rewrite letinE -uu'. + under eq_integral do rewrite retE /=. + over. +rewrite (sfinite_Fubini + [the {sfinite_measure set X -> \bar R} of T z] + [the {sfinite_measure set Y -> \bar R} of U z] + (fun x => \d_(x.1, x.2) A ))//; last first. + apply/measurable_EFinP => /=; rewrite (_ : (fun x => _) = mindic R mA)//. + by apply/funext => -[]. +rewrite /=. +apply: eq_integral => y _. +by rewrite letinE/= -tt'; apply: eq_integral => // x _; rewrite retE. +Qed. + +End letinC. + +(* examples *) + +Lemma letin_sample_bernoulli d d' (T : measurableType d) + (T' : measurableType d') (R : realType) (r : R) + (u : R.-sfker [the measurableType _ of (T * bool)%type] ~> T') x y : + (0 <= r <= 1)%R -> + letin (sample_cst (bernoulli r)) u x y = + r%:E * u (x, true) y + (`1- r)%:E * u (x, false) y. +Proof. by move=> r01; rewrite letinE/= integral_bernoulli. Qed. + +Section sample_and_return. +Import Notations. +Context d (T : measurableType d) (R : realType). + +Definition sample_and_return : R.-sfker T ~> _ := + letin + (sample_cst (bernoulli (2 / 7))) (* T -> B *) + (ret macc1of2) (* T * B -> B *). + +Lemma sample_and_returnE t U : sample_and_return t U = + (2 / 7%:R)%:E * \d_true U + (5%:R / 7%:R)%:E * \d_false U. +Proof. +rewrite /sample_and_return letin_sample_bernoulli; last lra. +by rewrite !retE onem27. +Qed. + +End sample_and_return. + +Section sample_and_branch. +Import Notations. +Context d (T : measurableType d) (R : realType). + +(* let x = sample (bernoulli (2/7)) in + let r = case x of {(1, _) => return (k3()), (2, _) => return (k10())} in + return r *) + +Definition sample_and_branch : R.-sfker T ~> _ := + letin + (sample_cst (bernoulli (2 / 7))) (* T -> B *) + (ite macc1of2 (ret (@k3 _ _ R)) (ret k10)). + +Lemma sample_and_branchE t U : sample_and_branch t U = + (2 / 7)%:E * \d_(3 : R) U + (5 / 7)%:E * \d_(10 : R) U. +Proof. +rewrite /sample_and_branch letin_sample_bernoulli/=; last lra. +by rewrite !iteE/= onem27. +Qed. + +End sample_and_branch. + +Section bernoulli_and. +Context d (T : measurableType d) (R : realType). +Import Notations. + +Definition bernoulli_and : R.-sfker T ~> mbool := + (letin (sample_cst (bernoulli (1 / 2))) + (letin (sample_cst (bernoulli (1 / 2))) + (ret (measurable_and macc1of3 macc2of3)))). + +Lemma bernoulli_andE t U : + bernoulli_and t U = sample_cst (bernoulli (1 / 4)) t U. +Proof. +rewrite /bernoulli_and. +rewrite letin_sample_bernoulli; last lra. +rewrite (letin_sample_bernoulli (r := 1 / 2)); last lra. +rewrite (letin_sample_bernoulli (r := 1 / 2)); last lra. +rewrite muleDr//= -muleDl//. +rewrite !muleA -addeA -muleDl// -!EFinM !onem1S/= -splitr mulr1. +have -> : (1 / 2 * (1 / 2) = 1 / 4%:R :> R)%R by rewrite mulf_div mulr1// -natrM. +rewrite [in RHS](_ : 1 / 4 = (1 / 4)%:nng%:num)%R//. +rewrite (bernoulli_dirac p14). +rewrite measure_addE/= /mscale/= -!EFinM; congr( _ + (_ * _)%:E). +have -> : (1 / 2 = 2 / 4%:R :> R)%R. + by apply/eqP; rewrite eqr_div// ?pnatr_eq0// mul1r -natrM. +by rewrite onem1S// -mulrDl. +Qed. + +End bernoulli_and. + +Section staton_bus. +Import Notations. +Context d (T : measurableType d) (R : realType) (h : R -> R). +Hypothesis mh : measurable_fun setT h. +Definition kstaton_bus : R.-sfker T ~> mbool := + letin (sample_cst (bernoulli (2 / 7))) + (letin + (letin (ite macc1of2 (ret k3) (ret k10)) + (score (measurableT_comp mh macc2of3))) + (ret macc1of3)). + +Definition staton_bus := normalize kstaton_bus. + +End staton_bus. + +(* let x = sample (bernoulli (2/7)) in + let r = case x of {(1, _) => return (k3()), (2, _) => return (k10())} in + let _ = score (1/4! r^4 e^-r) in + return x *) +Section staton_bus_poisson. +Import Notations. +Context d (T : measurableType d) (R : realType). +Let poisson4 := @poisson_pdf R 4%N. +Let mpoisson4 := @measurable_poisson_pdf R 4%N. + +Definition kstaton_bus_poisson : R.-sfker R ~> mbool := + kstaton_bus _ mpoisson4. + +Let kstaton_bus_poissonE t U : kstaton_bus_poisson t U = + (2 / 7)%:E * (poisson4 3)%:E * \d_true U + + (5 / 7)%:E * (poisson4 10)%:E * \d_false U. +Proof. +rewrite /kstaton_bus_poisson /kstaton_bus. +rewrite letin_sample_bernoulli; last lra. +rewrite -!muleA; congr (_ * _ + _ * _). +- rewrite letin_kret//. + rewrite letin_iteT//. + rewrite letin_retk//. + by rewrite scoreE//= => r r0; exact: poisson_pdf_ge0. +- by rewrite onem27. + rewrite letin_kret//. + rewrite letin_iteF//. + rewrite letin_retk//. + by rewrite scoreE//= => r r0; exact: poisson_pdf_ge0. +Qed. + +(* true -> 2/7 * 0.168 = 2/7 * 3^4 e^-3 / 4! *) +(* false -> 5/7 * 0.019 = 5/7 * 10^4 e^-10 / 4! *) + +Lemma staton_busE P (t : R) U : + let N := ((2 / 7) * poisson4 3 + + (5 / 7) * poisson4 10)%R in + staton_bus mpoisson4 P t U = + ((2 / 7)%:E * (poisson4 3)%:E * \d_true U + + (5 / 7)%:E * (poisson4 10)%:E * \d_false U) * N^-1%:E. +Proof. +rewrite /staton_bus normalizeE !kstaton_bus_poissonE !diracT !mule1 ifF //. +apply/negbTE; rewrite gt_eqF// lte_fin. +by rewrite addr_gt0// mulr_gt0//= ?divr_gt0// ?ltr0n// poisson_pdf_gt0// ltr0n. +Qed. + +End staton_bus_poisson. + +(* let x = sample (bernoulli (2/7)) in + let r = case x of {(1, _) => return (k3()), (2, _) => return (k10())} in + let _ = score (r e^-(15/60 r)) in + return x *) +Section staton_bus_exponential. +Import Notations. +Context d (T : measurableType d) (R : realType). +Let exp1560 := @exponential_pdf R (ratr (15%:Q / 60%:Q)). +Let mexp1560 := @measurable_exponential_pdf R (ratr (15%:Q / 60%:Q)). + +(* 15/60 = 0.25 *) + +Definition kstaton_bus_exponential : R.-sfker R ~> mbool := + kstaton_bus _ mexp1560. + +Let kstaton_bus_exponentialE t U : kstaton_bus_exponential t U = + (2 / 7)%:E * (exp1560 3)%:E * \d_true U + + (5 / 7)%:E * (exp1560 10)%:E * \d_false U. +Proof. +rewrite /kstaton_bus. +rewrite letin_sample_bernoulli; last lra. +rewrite -!muleA; congr (_ * _ + _ * _). +- rewrite letin_kret//. + rewrite letin_iteT//. + rewrite letin_retk//. + rewrite scoreE//= => r r0; exact: exponential_pdf_ge0. +- by rewrite onem27. + rewrite letin_kret//. + rewrite letin_iteF//. + rewrite letin_retk//. + by rewrite scoreE//= => r r0; exact: exponential_pdf_ge0. +Qed. + +(* true -> 5/7 * 0.019 = 5/7 * 10^4 e^-10 / 4! *) +(* false -> 2/7 * 0.168 = 2/7 * 3^4 e^-3 / 4! *) + +Lemma staton_bus_exponentialE P (t : R) U : + let N := ((2 / 7) * exp1560 3 + + (5 / 7) * exp1560 10)%R in + staton_bus mexp1560 P t U = + ((2 / 7)%:E * (exp1560 3)%:E * \d_true U + + (5 / 7)%:E * (exp1560 10)%:E * \d_false U) * N^-1%:E. +Proof. +rewrite /staton_bus. +rewrite normalizeE /= !kstaton_bus_exponentialE !diracT !mule1 ifF //. +apply/negbTE; rewrite gt_eqF// lte_fin. +by rewrite addr_gt0// mulr_gt0//= ?divr_gt0// ?ltr0n// exponential_pdf_gt0 ?ltr0n. +Qed. + +End staton_bus_exponential. + (**md letin' variants *) From 8a5e466576203703e743a1dff770bee0b0f1e8ef Mon Sep 17 00:00:00 2001 From: Reynald Affeldt Date: Fri, 3 Jan 2025 11:08:40 +0900 Subject: [PATCH 36/36] minor fix --- theories/kernel.v | 4 +- theories/lang_syntax.v | 9 +- theories/lebesgue_integral.v | 2 +- theories/prob_lang.v | 357 +---------------------------------- theories/prob_lang_wip.v | 5 +- 5 files changed, 13 insertions(+), 364 deletions(-) diff --git a/theories/kernel.v b/theories/kernel.v index 8b9d428a8..86fa740c6 100644 --- a/theories/kernel.v +++ b/theories/kernel.v @@ -1566,14 +1566,14 @@ Definition mkproduct := End kproduct_measure. -(* [Theorem 14.22, Klenke 2014] (3/3): the composition of finite transition - kernels is sigma-finite *) HB.instance Definition _ d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} (k1 : R.-ftker T0 ~> T1) (k2 : R.-ftker (T0 * T1) ~> T2) := @isKernel.Build _ _ T0 (T1 * T2)%type R (mkproduct k1 k2) (measurable_kproduct k1 k2). +(* [Theorem 14.22, Klenke 2014] (3/3): the composition of finite transition + kernels is sigma-finite *) Section sigma_finite_kproduct. Context d0 d1 d2 (T0 : measurableType d0) (T1 : measurableType d1) (T2 : measurableType d2) {R : realType} diff --git a/theories/lang_syntax.v b/theories/lang_syntax.v index 5099b8d1b..5ce4e20a6 100644 --- a/theories/lang_syntax.v +++ b/theories/lang_syntax.v @@ -1337,7 +1337,8 @@ Lemma beta_pdf_uniq_ae (a b : nat) : (EFin \o (beta_pdf a b)). Proof. apply: integral_ae_eq => //. -- apply: integrableS (Radon_Nikodym_integrable _) => //. +- apply: (@integrableS _ _ _ _ setT) => //=. + apply: Radon_Nikodym_integrable => //=. exact: beta_prob_dom. - apply/measurable_funTS/measurableT_comp => //. exact: measurable_beta_pdf. @@ -1366,9 +1367,9 @@ move=> mU mf finf. rewrite -(Radon_Nikodym_change_of_variables (beta_prob_dom a b)) //=; last first. by apply/integrableP; split. apply: ae_eq_integral => //. -- apply: emeasurable_funM => //; apply: measurable_int. - apply: integrableS (Radon_Nikodym_integrable _) => //=. - exact: beta_prob_dom. +- apply: emeasurable_funM => //; apply: (measurable_int mu). + apply: (integrableS _ _ (@subsetT _ _)) => //=. + by apply: Radon_Nikodym_integrable; exact: beta_prob_dom. - apply: emeasurable_funM => //=; apply/measurableT_comp => //=. by apply/measurable_funTS; exact: measurable_beta_pdf. - apply: ae_eq_mul2l => /=. diff --git a/theories/lebesgue_integral.v b/theories/lebesgue_integral.v index 284f18536..d02164a02 100644 --- a/theories/lebesgue_integral.v +++ b/theories/lebesgue_integral.v @@ -5950,7 +5950,7 @@ Context d d' (X : measurableType d) (Y : measurableType d') (R : realType). Variables (m1 : {sfinite_measure set X -> \bar R}). Variables (m2 : {sfinite_measure set Y -> \bar R}). Variables (f : X * Y -> \bar R) (f0 : forall xy, 0 <= f xy). -Hypothesis mf : measurable_fun setT f. +Hypothesis mf : measurable_fun [set: X * Y] f. Lemma sfinite_Fubini : \int[m1]_x \int[m2]_y f (x, y) = \int[m2]_y \int[m1]_x f (x, y). diff --git a/theories/prob_lang.v b/theories/prob_lang.v index 24ae6a524..56cc11e21 100644 --- a/theories/prob_lang.v +++ b/theories/prob_lang.v @@ -1466,8 +1466,8 @@ by rewrite /score/= /mscale/= ger0_norm//= f0. Qed. Lemma score_score (f : R -> R) (g : R * unit -> R) - (mf : measurable_fun setT f) - (mg : measurable_fun setT g) : + (mf : measurable_fun [set: R] f) + (mg : measurable_fun [set: R * unit] g) : letin (score mf) (score mg) = score (measurable_funM mf (measurableT_comp mg (measurable_pair2 tt))). Proof. @@ -1916,359 +1916,6 @@ End von_neumann_trick_proof. End von_neumann_trick. -Section insn1_lemmas. -Import Notations. -Context d (T : measurableType d) (R : realType). - -Let kcomp_scoreE d1 d2 (T1 : measurableType d1) (T2 : measurableType d2) - (g : R.-sfker [the measurableType _ of (T1 * unit)%type] ~> T2) - f (mf : measurable_fun setT f) r U : - (score mf \; g) r U = `|f r|%:E * g (r, tt) U. -Proof. -rewrite /= /kcomp /kscore /= ge0_integral_mscale//=. -by rewrite integral_dirac// diracT mul1e. -Qed. - -Lemma scoreE d' (T' : measurableType d') (x : T * T') (U : set T') (f : R -> R) - (r : R) (r0 : (0 <= r)%R) - (f0 : (forall r, 0 <= r -> 0 <= f r)%R) (mf : measurable_fun setT f) : - score (measurableT_comp mf (@macc1of2 _ _ _ _)) - (x, r) (curry (snd \o fst) x @^-1` U) = - (f r)%:E * \d_x.2 U. -Proof. -by rewrite /score/= /mscale/= ger0_norm//= f0. -Qed. - -Lemma score_score (f : R -> R) (g : R * unit -> R) - (mf : measurable_fun setT f) - (mg : measurable_fun setT g) : - letin (score mf) (score mg) = - score (measurable_funM mf (measurableT_comp mg (measurable_pair2 tt))). -Proof. -apply/eq_sfkernel => x U. -rewrite {1}/letin; unlock. -by rewrite kcomp_scoreE/= /mscale/= diracE normrM muleA EFinM. -Qed. - -(* hard constraints to express score below 1 *) -Lemma score_fail (r : R) : (0 <= r <= 1)%R -> - score (kr r) = - letin (sample_cst (bernoulli r) : R.-pker T ~> _) - (ite (@macc1of2 _ _ _ _) (ret ktt) fail). -Proof. -move=> /andP[r0 r1]; apply/eq_sfkernel => x U. -rewrite letinE/= /sample; unlock. -by rewrite /mscale/= ger0_norm// integral_bernoulli ?r0//= 2!iteE//= failE mule0 adde0. -Qed. - -End insn1_lemmas. - -Section letin_ite. -Context d d2 d3 (T : measurableType d) (T2 : measurableType d2) - (Z : measurableType d3) (R : realType). -Variables (k1 k2 : R.-sfker T ~> Z) - (u : R.-sfker [the measurableType _ of (T * Z)%type] ~> T2) - (f : T -> bool) (mf : measurable_fun setT f) - (t : T) (U : set T2). - -Lemma letin_iteT : f t -> letin (ite mf k1 k2) u t U = letin k1 u t U. -Proof. -move=> ftT; rewrite !letinE/=; apply: eq_measure_integral => V mV _. -by rewrite iteE ftT. -Qed. - -Lemma letin_iteF : ~~ f t -> letin (ite mf k1 k2) u t U = letin k2 u t U. -Proof. -move=> ftF; rewrite !letinE/=; apply: eq_measure_integral => V mV _. -by rewrite iteE (negbTE ftF). -Qed. - -End letin_ite. - -Section letinA. -Context d d' d1 d2 d3 (X : measurableType d) (Y : measurableType d') - (T1 : measurableType d1) (T2 : measurableType d2) (T3 : measurableType d3) - (R : realType). -Import Notations. -Variables (t : R.-sfker X ~> T1) - (u : R.-sfker (X * T1) ~> T2) - (v : R.-sfker (X * T2) ~> Y) - (v' : R.-sfker (X * T1 * T2) ~> Y) - (vv' : forall y, v =1 fun xz => v' (xz.1, y, xz.2)). - -Lemma letinA x A : measurable A -> - letin t (letin u v') x A - = - (letin (letin t u) v) x A. -Proof. -move=> mA. -rewrite !letinE. -under eq_integral do rewrite letinE. -rewrite integral_kcomp; [|by []|]. -- apply: eq_integral => y _. - apply: eq_integral => z _. - by rewrite (vv' y). -- exact: (measurableT_comp (measurable_kernel v _ mA)). -Qed. - -End letinA. - -Section letinC. -Context d d1 d' (X : measurableType d) (Y : measurableType d1) - (Z : measurableType d') (R : realType). - -Import Notations. - -Variables (t : R.-sfker Z ~> X) - (t' : R.-sfker [the measurableType _ of (Z * Y)%type] ~> X) - (tt' : forall y, t =1 fun z => t' (z, y)) - (u : R.-sfker Z ~> Y) - (u' : R.-sfker [the measurableType _ of (Z * X)%type] ~> Y) - (uu' : forall x, u =1 fun z => u' (z, x)). - -Definition T z : set X -> \bar R := t z. -Let T0 z : (T z) set0 = 0. Proof. by []. Qed. -Let T_ge0 z x : 0 <= (T z) x. Proof. by []. Qed. -Let T_semi_sigma_additive z : semi_sigma_additive (T z). -Proof. exact: measure_semi_sigma_additive. Qed. -HB.instance Definition _ z := @isMeasure.Build _ X R (T z) (T0 z) (T_ge0 z) - (@T_semi_sigma_additive z). - -Let sfinT z : sfinite_measure (T z). Proof. exact: sfinite_kernel_measure. Qed. -HB.instance Definition _ z := @isSFinite.Build _ X R (T z) (sfinT z). - -Definition U z : set Y -> \bar R := u z. -Let U0 z : (U z) set0 = 0. Proof. by []. Qed. -Let U_ge0 z x : 0 <= (U z) x. Proof. by []. Qed. -Let U_semi_sigma_additive z : semi_sigma_additive (U z). -Proof. exact: measure_semi_sigma_additive. Qed. -HB.instance Definition _ z := @isMeasure.Build _ Y R (U z) (U0 z) (U_ge0 z) - (@U_semi_sigma_additive z). - -Let sfinU z : sfinite_measure (U z). Proof. exact: sfinite_kernel_measure. Qed. -HB.instance Definition _ z := @isSFinite.Build _ Y R (U z) (sfinU z). - -Lemma letinC z A : measurable A -> - letin t - (letin u' - (ret (measurable_fun_prod macc1of3 macc2of3))) z A = - letin u - (letin t' - (ret (measurable_fun_prod macc2of3 macc1of3))) z A. -Proof. -move=> mA. -rewrite !letinE. -under eq_integral. - move=> x _. - rewrite letinE -uu'. - under eq_integral do rewrite retE /=. - over. -rewrite (sfinite_Fubini - [the {sfinite_measure set X -> \bar R} of T z] - [the {sfinite_measure set Y -> \bar R} of U z] - (fun x => \d_(x.1, x.2) A ))//; last first. - apply/measurable_EFinP => /=; rewrite (_ : (fun x => _) = mindic R mA)//. - by apply/funext => -[]. -rewrite /=. -apply: eq_integral => y _. -by rewrite letinE/= -tt'; apply: eq_integral => // x _; rewrite retE. -Qed. - -End letinC. - -(* examples *) - -Lemma letin_sample_bernoulli d d' (T : measurableType d) - (T' : measurableType d') (R : realType) (r : R) - (u : R.-sfker [the measurableType _ of (T * bool)%type] ~> T') x y : - (0 <= r <= 1)%R -> - letin (sample_cst (bernoulli r)) u x y = - r%:E * u (x, true) y + (`1- r)%:E * u (x, false) y. -Proof. by move=> r01; rewrite letinE/= integral_bernoulli. Qed. - -Section sample_and_return. -Import Notations. -Context d (T : measurableType d) (R : realType). - -Definition sample_and_return : R.-sfker T ~> _ := - letin - (sample_cst (bernoulli (2 / 7))) (* T -> B *) - (ret macc1of2) (* T * B -> B *). - -Lemma sample_and_returnE t U : sample_and_return t U = - (2 / 7%:R)%:E * \d_true U + (5%:R / 7%:R)%:E * \d_false U. -Proof. -rewrite /sample_and_return letin_sample_bernoulli; last lra. -by rewrite !retE onem27. -Qed. - -End sample_and_return. - -Section sample_and_branch. -Import Notations. -Context d (T : measurableType d) (R : realType). - -(* let x = sample (bernoulli (2/7)) in - let r = case x of {(1, _) => return (k3()), (2, _) => return (k10())} in - return r *) - -Definition sample_and_branch : R.-sfker T ~> _ := - letin - (sample_cst (bernoulli (2 / 7))) (* T -> B *) - (ite macc1of2 (ret (@k3 _ _ R)) (ret k10)). - -Lemma sample_and_branchE t U : sample_and_branch t U = - (2 / 7)%:E * \d_(3 : R) U + (5 / 7)%:E * \d_(10 : R) U. -Proof. -rewrite /sample_and_branch letin_sample_bernoulli/=; last lra. -by rewrite !iteE/= onem27. -Qed. - -End sample_and_branch. - -Section bernoulli_and. -Context d (T : measurableType d) (R : realType). -Import Notations. - -Definition bernoulli_and : R.-sfker T ~> mbool := - (letin (sample_cst (bernoulli (1 / 2))) - (letin (sample_cst (bernoulli (1 / 2))) - (ret (measurable_and macc1of3 macc2of3)))). - -Lemma bernoulli_andE t U : - bernoulli_and t U = sample_cst (bernoulli (1 / 4)) t U. -Proof. -rewrite /bernoulli_and. -rewrite letin_sample_bernoulli; last lra. -rewrite (letin_sample_bernoulli (r := 1 / 2)); last lra. -rewrite (letin_sample_bernoulli (r := 1 / 2)); last lra. -rewrite muleDr//= -muleDl//. -rewrite !muleA -addeA -muleDl// -!EFinM !onem1S/= -splitr mulr1. -have -> : (1 / 2 * (1 / 2) = 1 / 4%:R :> R)%R by rewrite mulf_div mulr1// -natrM. -rewrite [in RHS](_ : 1 / 4 = (1 / 4)%:nng%:num)%R//. -rewrite (bernoulli_dirac p14). -rewrite measure_addE/= /mscale/= -!EFinM; congr( _ + (_ * _)%:E). -have -> : (1 / 2 = 2 / 4%:R :> R)%R. - by apply/eqP; rewrite eqr_div// ?pnatr_eq0// mul1r -natrM. -by rewrite onem1S// -mulrDl. -Qed. - -End bernoulli_and. - -Section staton_bus. -Import Notations. -Context d (T : measurableType d) (R : realType) (h : R -> R). -Hypothesis mh : measurable_fun setT h. -Definition kstaton_bus : R.-sfker T ~> mbool := - letin (sample_cst (bernoulli (2 / 7))) - (letin - (letin (ite macc1of2 (ret k3) (ret k10)) - (score (measurableT_comp mh macc2of3))) - (ret macc1of3)). - -Definition staton_bus := normalize kstaton_bus. - -End staton_bus. - -(* let x = sample (bernoulli (2/7)) in - let r = case x of {(1, _) => return (k3()), (2, _) => return (k10())} in - let _ = score (1/4! r^4 e^-r) in - return x *) -Section staton_bus_poisson. -Import Notations. -Context d (T : measurableType d) (R : realType). -Let poisson4 := @poisson_pdf R 4%N. -Let mpoisson4 := @measurable_poisson_pdf R 4%N. - -Definition kstaton_bus_poisson : R.-sfker R ~> mbool := - kstaton_bus _ mpoisson4. - -Let kstaton_bus_poissonE t U : kstaton_bus_poisson t U = - (2 / 7)%:E * (poisson4 3)%:E * \d_true U + - (5 / 7)%:E * (poisson4 10)%:E * \d_false U. -Proof. -rewrite /kstaton_bus_poisson /kstaton_bus. -rewrite letin_sample_bernoulli; last lra. -rewrite -!muleA; congr (_ * _ + _ * _). -- rewrite letin_kret//. - rewrite letin_iteT//. - rewrite letin_retk//. - by rewrite scoreE//= => r r0; exact: poisson_pdf_ge0. -- by rewrite onem27. - rewrite letin_kret//. - rewrite letin_iteF//. - rewrite letin_retk//. - by rewrite scoreE//= => r r0; exact: poisson_pdf_ge0. -Qed. - -(* true -> 2/7 * 0.168 = 2/7 * 3^4 e^-3 / 4! *) -(* false -> 5/7 * 0.019 = 5/7 * 10^4 e^-10 / 4! *) - -Lemma staton_busE P (t : R) U : - let N := ((2 / 7) * poisson4 3 + - (5 / 7) * poisson4 10)%R in - staton_bus mpoisson4 P t U = - ((2 / 7)%:E * (poisson4 3)%:E * \d_true U + - (5 / 7)%:E * (poisson4 10)%:E * \d_false U) * N^-1%:E. -Proof. -rewrite /staton_bus normalizeE !kstaton_bus_poissonE !diracT !mule1 ifF //. -apply/negbTE; rewrite gt_eqF// lte_fin. -by rewrite addr_gt0// mulr_gt0//= ?divr_gt0// ?ltr0n// poisson_pdf_gt0// ltr0n. -Qed. - -End staton_bus_poisson. - -(* let x = sample (bernoulli (2/7)) in - let r = case x of {(1, _) => return (k3()), (2, _) => return (k10())} in - let _ = score (r e^-(15/60 r)) in - return x *) -Section staton_bus_exponential. -Import Notations. -Context d (T : measurableType d) (R : realType). -Let exp1560 := @exponential_pdf R (ratr (15%:Q / 60%:Q)). -Let mexp1560 := @measurable_exponential_pdf R (ratr (15%:Q / 60%:Q)). - -(* 15/60 = 0.25 *) - -Definition kstaton_bus_exponential : R.-sfker R ~> mbool := - kstaton_bus _ mexp1560. - -Let kstaton_bus_exponentialE t U : kstaton_bus_exponential t U = - (2 / 7)%:E * (exp1560 3)%:E * \d_true U + - (5 / 7)%:E * (exp1560 10)%:E * \d_false U. -Proof. -rewrite /kstaton_bus. -rewrite letin_sample_bernoulli; last lra. -rewrite -!muleA; congr (_ * _ + _ * _). -- rewrite letin_kret//. - rewrite letin_iteT//. - rewrite letin_retk//. - rewrite scoreE//= => r r0; exact: exponential_pdf_ge0. -- by rewrite onem27. - rewrite letin_kret//. - rewrite letin_iteF//. - rewrite letin_retk//. - by rewrite scoreE//= => r r0; exact: exponential_pdf_ge0. -Qed. - -(* true -> 5/7 * 0.019 = 5/7 * 10^4 e^-10 / 4! *) -(* false -> 2/7 * 0.168 = 2/7 * 3^4 e^-3 / 4! *) - -Lemma staton_bus_exponentialE P (t : R) U : - let N := ((2 / 7) * exp1560 3 + - (5 / 7) * exp1560 10)%R in - staton_bus mexp1560 P t U = - ((2 / 7)%:E * (exp1560 3)%:E * \d_true U + - (5 / 7)%:E * (exp1560 10)%:E * \d_false U) * N^-1%:E. -Proof. -rewrite /staton_bus. -rewrite normalizeE /= !kstaton_bus_exponentialE !diracT !mule1 ifF //. -apply/negbTE; rewrite gt_eqF// lte_fin. -by rewrite addr_gt0// mulr_gt0//= ?divr_gt0// ?ltr0n// exponential_pdf_gt0 ?ltr0n. -Qed. - -End staton_bus_exponential. - (**md letin' variants *) diff --git a/theories/prob_lang_wip.v b/theories/prob_lang_wip.v index e36d17297..e084df971 100644 --- a/theories/prob_lang_wip.v +++ b/theories/prob_lang_wip.v @@ -168,8 +168,9 @@ apply: ae_eq_integral => //=. - apply: emeasurable_funM => //. apply/measurable_funTS/measurableT_comp => //. exact: measurable_fun_f1. - apply: measurable_int. - apply: integrableS (Radon_Nikodym_integrable _) => //=. + apply: (measurable_int mu). + apply: (integrableS _ _ (@subsetT _ _)) => //=. + apply: Radon_Nikodym_integrable => //=. exact: gauss01_dom. - apply: emeasurable_funM => //. apply/measurable_funTS/measurableT_comp => //.