Module mathcomp.analysis.probability_theory.subpmf

From HB Require Import structures.
From mathcomp Require Import boot order algebra.
#[warning="-warn-library-file-internal-analysis"]
From mathcomp Require Import unstable.
From mathcomp Require Import boolp classical_sets functions filter.
From mathcomp Require Import cardinality fsbigop reals.
From mathcomp Require Import topology normedtype sequences esum ereal numfun.
From mathcomp Require Import measure lebesgue_measure measurable_realfun
  lebesgue_integral giry hoelder random_variable.

# Subprobability mass function Caution: This file is experimental. The interface may change in the future. `R.-pmf T` : subprobability mass function for choiceType `T` with values in `R` `isSubPmf` : interface for functions that satisfy the property of subprobability : mass function `\P_[mu] E` : probability of the event `E` w.r.t. the subpmf `mu` `\P_[mu, A] E` : probability of the event `E` conditioned on the event `A` `\E_[mu] f` : expectation of the real-valued function `f` w.r.t. the subpmf `mu` `subpmf_sum` : alternative characterization of subpmf in terms of uniq lists `pmf_esummable mu f` : summability of the function `f` weighted by the subpmf `mu` `pmf_zero` : the zero subpmf `pmf_restr p mu` : restriction of `mu` to the predicate `p`, zero outside of it `pmf_count s` : subpmf on the elements of the sequence `s`, each weighted by its : number of occurrences in `s` `pmf_ret x` : degenerated subpmf `pmf_uniform s` : uniform subpmf on the elements of the sequence `s` `pmf_flip r` : subpmf on booleans, `true` with probability `r` clamped to $[0, 1]$ `\bind_(i <- d) E` : monadic bind for subpmf `pmf_bindr f` : reverse monadic bind `pmf_iter n f` : `n`-fold iteration of `pmf_bindr f` from a degenerated subpmf `\lim_(n) E` : limit for index subpmf `pmf_margin h mu` : image of the subpmf `mu` along the function `h` `pmf_swap mu` : image of a subpmf on pairs under the swapping of the components

Reserved Notation "R '.-pmf' T"
  (at level 1, T at level 2, format "R '.-pmf' T ").
Reserved Notation "\P_[ mu ] E" (at level 2, format "\P_[ mu ] E").
Reserved Notation "\P_[ mu , A ] E" (at level 2, format "\P_[ mu , A ] E").
Reserved Notation "\E_[ mu ] f" (at level 2, format "\E_[ mu ] f").
Reserved Notation "\E_[ mu , A ] f" (at level 2, format "\E_[ mu , A ] f").
Reserved Notation "\bind_ ( i <- d ) E"
  (at level 36, E at level 36, i, d at level 50,
     format "'[' \bind_ ( i <- d ) '/ ' E ']'").
Reserved Notation "\lim_ ( n ) E"
  (at level 36, E at level 36, n at level 50,
     format "'[' \lim_ ( n ) '/ ' E ']'").

Declare Scope subpmf_scope.

Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.

Import Order.TTheory GRing.Theory Num.Theory.

Local Open Scope ring_scope.
Local Open Scope classical_set_scope.

Local Notation "f '<=1' g"Source code := (forall xSource code, f x <= g x)
  (at level 70, no associativity).
Local Notation "f '<=2' g"Source code := (forall xSource code ySource code, f x y <= g x y)
  (at level 70, no associativity).

Definition discrete_measurable_space

discrete_measurable_space : choiceType -> Type discrete_measurable_space is not universe polymorphic Arguments discrete_measurable_space T discrete_measurable_space is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.discrete_measurable_space Declared in library mathcomp.analysis.probability_theory.subpmf, line 107, characters 11-36


Source code
 (TSource code : choiceType) : Type := T.

HBSource code.instanceSource code DefinitionSource code _Source code (TSource code :Source code choiceTypeSource code) :=
  Choice.on (discrete_measurable_space T).

HBSource code.instanceSource code DefinitionSource code _Source code (TSource code :Source code choiceTypeSource code) := @isMeasurable.Build
  default_measure_display (discrete_measurable_space T)
  discrete_measurable discrete_measurable0
  discrete_measurableC discrete_measurableU.

Lemma esum_setT_discreteSource code {RSource code : realType} {TSource code : choiceType} (fSource code : T -> \bar R) :
  \esum_(xSource code in [set: discrete_measurable_space T]) f x =
  \esum_(xSource code in [set: T]) f x.
Proof.
by apply: (@reindex_esum R T (discrete_measurable_space T) setT setT id); split.
Qed.

Lemma measurable_discreteSource code {TSource code : choiceType}
    (ASource code : set (discrete_measurable_space T)) :
  measurable A.
Proof.
by []. Qed.

#[global] Hint Resolve measurable_discrete : core.

Lemma measurable_fun_discreteSource code {TSource code : choiceType} {d'Source code} {USource code : sigmaRingType d'}
    (DSource code : set (discrete_measurable_space T))
    (fSource code : discrete_measurable_space T -> U) :
  measurable_fun D f.
Proof.
by []. Qed.

#[global] Hint Resolve measurable_fun_discrete : core.

Lemma measurable_fun_discreteTSource code {TSource code USource code : choiceType} (hSource code : T -> U) :
  measurable_fun [set: discrete_measurable_space T]
    (h : discrete_measurable_space T -> discrete_measurable_space U).
Proof.
by []. Qed.

HBSource code.mixinSource code RecordSource code isSubPmfSource code {RSource code :Source code realType}Source code {T : choiceType} (mu : T -> R) := {
  subpmf_ge0 : forall xSource code, 0 <= mu x ;
  subpmf_summable : esummable [set: T] (EFin \o mu);
  subpmf_sum_le1 : (esum [set: T] (EFin \o mu) <= 1)%E }.

HBSource code.structureSource code DefinitionSource code SubPmfSource code {RSource code :Source code realType}Source code {T : choiceType} :=
  {fSource code of @isSubPmf R T f}.

Arguments subpmf_ge0 {R T} _.
Arguments subpmf_sum_le1 {R T} _.

#[global] Hint Extern 0 (is_true (0 <= _)) => solve [apply: subpmf_ge0] : core.
#[global] Hint Resolve subpmf_summable : core.

Notation "R '.-pmf' T"Source code := (@SubPmf.type R T) : subpmf_scope.
Local Open Scope subpmf_scope.

Lemma subpmf_eqPSource code {RSource code : realType} {TSource code : choiceType} (f1Source code f2Source code : R.-pmf T) :
  f1 =1 f2 <-> f1 = f2.
Proof.
split=> [|->] //.
case: f1 => f1 [[]ge0_1 s1 le1_1].
case: f2 => f2 [[]ge0_2 s2 le1_2].
move => /funext /= h.
by subst; do 3 f_equal.
Qed.

Definition measure_of_pmf

measure_of_pmf : forall {R : realType} {T : choiceType}, (T -> R) -> set (discrete_measurable_space T) -> GRing.BaseAddMagma.sort \bar R measure_of_pmf is not universe polymorphic Arguments measure_of_pmf {R T} mu%_function_scope S%_classical_set_scope measure_of_pmf is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.measure_of_pmf Declared in library mathcomp.analysis.probability_theory.subpmf, line 171, characters 11-25


Source code
{RSource code : realType} {TSource code : choiceType} (muSource code : T -> R)
    (SSource code : set (discrete_measurable_space T)) :=
  \esum_(xSource code in S) (EFin \o mu) x.

Section SubProbability_measure_subpmf.
Context {RSource code : realType} {TSource code : choiceType} (muSource code : R.-pmf T).

Let P0Source code : measure_of_pmf mu set0 = 0.
Proof.
by rewrite /measure_of_pmf esum_set0. Qed.

Let P_ge0Source code (SSource code : set (discrete_measurable_space T)) : (0 <= measure_of_pmf mu S)%E.
Proof.
by apply: esum_ge0 => x _; rewrite lee_fin. Qed.

Let P_sigma_additiveSource code : semi_sigma_additive (measure_of_pmf mu).
Proof.
move=> F _ tF _.
rewrite [X in _ --> X](_ : _ = \sum_(iSource code <oo) measure_of_pmf mu (F i))%E.
  rewrite nneseries_esumT//; apply: esum_bigcup => //=.
    exact: (sub_trivIset (@subsetT _ _)).
  by move=> x; rewrite lee_fin.
by apply: is_cvg_nneseries => n _ _; apply: esum_ge0 => x _; rewrite lee_fin.
Qed.

HBSource code.instanceSource code DefinitionSource code _Source code :=Source code isMeasureSource code.Build _ _ _ (measure_of_pmf mu)
  P0 P_ge0 P_sigma_additive.

Let P_setTSource code : (measure_of_pmf mu [set: discrete_measurable_space T] <= 1)%E.
Proof.

HBSource code.instanceSource code DefinitionSource code _Source code :=Source code
  @Measure_isSubProbabilitySource code.Build _ _ R (measure_of_pmf mu) P_setT.

End SubProbability_measure_subpmf.

Section expectation_measure_of_pmf.
Context {RSource code : realType} {TSource code : choiceType}.
Implicit Types (mu : R.-pmf T).

Local Open Scope ereal_scope.

Lemma integral_measure_of_pmfSource code muSource code (fSource code : T -> \bar R) : (forall xSource code, 0 <= f x) ->
  \int[measure_of_pmf mu]_xSource code f x = \esum_(xSource code in [set: T]) (f x * (mu x)%:E).
Proof.

Lemma esummable_integrableSource code muSource code (ASource code : T -> \bar R) :
  esummable [set: T] (fun xSource code : T => A x * (mu x)%:E)%E <->
  (measure_of_pmf mu).-integrable [set: discrete_measurable_space T] A.
Proof.
rewrite esummableE.
under eq_esum do rewrite abseM abse_EFin (@ger0_norm _ (mu _))//.
rewrite ge0_fin_numE//.
  by rewrite esum_ge0// => x _; rewrite mule_ge0 ?lee_fin ?abse_ge0.
split=> [Amu|/integrableP[_]].
  apply/integrableP; split => //.
  by rewrite integral_measure_of_pmf// => x; rewrite abse_ge0.
by rewrite integral_measure_of_pmf// => x; rewrite abse_ge0.
Qed.

Lemma expectation_pmfSource code muSource code (fSource code : T -> \bar R) :
  'E_(measure_of_pmf mu)[f] = \esum_(xSource code in [set:T]) (f x * (mu x)%:E).
Proof.
rewrite expectation_def integralE esumE; congr (_ - _).
- rewrite integral_measure_of_pmf//; apply: eq_esum => x _.
  by rewrite !funeposE maxe_pMl ?mul0e// lee_fin.
- rewrite integral_measure_of_pmf//; apply: eq_esum => x _.
  by rewrite !funenegE maxe_pMl ?mul0e ?mulNe// lee_fin.
Qed.

Lemma expectation_pmfEFinSource code muSource code (gSource code : T -> R) :
  'E_(measure_of_pmf mu) [EFin \o g] = \esum_(xSource code in [set: T]) (g x * mu x)%:E.
Proof.
by rewrite expectation_pmf. Qed.

Lemma expectation_pmf0Source code muSource code : 'E_(measure_of_pmf mu) [cst 0] = 0.
Proof.
by rewrite unlock integral0. Qed.

End expectation_measure_of_pmf.

HBSource code.factorySource code RecordSource code uniq_isSubPmfSource code {RSource code :Source code realType}Source code {T : choiceType} (mu : T -> R) := {
  subpmf_ge0 : forall xSource code : T, 0 <= mu x ;
  uniq_subpmf_le1 : forall JSource code, uniq J -> \sum_(jSource code <- J) mu j <= 1 }.

HBSource code.buildersSource code ContextSource code {RSource code :Source code realType}Source code {T : choiceType} (mu : T -> R)
  & @uniq_isSubPmf R T mu.

Local Lemma pmf_ge0Source code xSource code : 0 <= mu x.
Proof.
exact: subpmf_ge0. Qed.

Local Lemma pmf_summableSource code : esummable [set: T] (EFin \o mu).
Proof.
rewrite /esummable (@le_lt_trans _ _ 1%:E) ?ltey// ge0_esum//.
  by move => ? _; rewrite lee_fin.
rewrite ge_ereal_sup//= => _ [X [finX _]] <-.
rewrite fsumEFin// lee_fin fsbig_finite//=.
rewrite (eq_bigr mu) ?uniq_subpmf_le1//.
by move => ? _; rewrite ger0_norm// subpmf_ge0.
Qed.

Local Lemma pmf_le1Source code : (esum [set: T] (EFin \o mu) <= 1)%E.
Proof.
rewrite ge0_esum.
  by move => ? _; rewrite lee_fin subpmf_ge0.
rewrite ge_ereal_sup//= => _ [X [finX _]] <-.
by rewrite fsumEFin// lee_fin fsbig_finite//= uniq_subpmf_le1.
Qed.

HBSource code.instanceSource code DefinitionSource code _Source code :=Source code @isSubPmfSource code.Build R T mu pmf_ge0 pmf_summable pmf_le1.

HBSource code.endSource code.

Section pmf_subpmf.
Import MeasurableR.
Context {dSource code} {TSource code : measurableType d} {RSource code : realType} (PSource code : probability T R)
  (XSource code : {RV P >-> R}).

Let pmf_fin_bigcupSource code (JSource code : seq R) : uniq J ->
  \sum_(jSource code <- J) (pmf X j)%:E = P (\bigcup_(jSource code in [set` J]) X @^-1` [set j]).
Proof.
move=> uJ.
rewrite (@measure_fin_bigcup _ _ _ P _ [set` J] (fun jSource code => X @^-1` [set j]))//.
- exact: finite_seq.
- exact: trivIset_preimage1.
- rewrite fsbig_seq//; apply: eq_fsbigr => j _.
  by rewrite /pmf fineK// fin_num_measure.
Qed.

Let pmf_uniq_le1Source code (JSource code : seq R) : uniq J -> (\sum_(jSource code <- J) pmf X j <= 1)%R.
Proof.
move=> uJ; rewrite -lee_fin -sumEFin pmf_fin_bigcup//.
by rewrite probability_le1//; exact: fin_bigcup_measurable.
Qed.

HBSource code.instanceSource code DefinitionSource code _Source code :=Source code
  @uniq_isSubPmfSource code.Build R R (pmf X) (@pmf_ge0 _ _ _ P X) pmf_uniq_le1.

End pmf_subpmf.

Lemma subpmf_le1Source code {RSource code : realType} {TSource code : choiceType} (muSource code : R.-pmf T) xSource code : mu x <= 1.
Proof.
rewrite -lee_fin; apply: le_trans; last exact: subpmf_sum_le1 mu.
by rewrite esum_ge1// => ? _; rewrite lee_fin.
Qed.

Section StdDefs.
Context {RSource code : realType} (TSource code : choiceType).
Implicit Types (mu : R.-pmf T) (A B E : pred T) (f : T -> R).

Definition pmf_prob

pmf_prob : forall {R : realType} [T : choiceType], SubPmf.type R T -> pred T -> R pmf_prob is not universe polymorphic Arguments pmf_prob {R} [T] mu E pmf_prob is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_prob Declared in library mathcomp.analysis.probability_theory.subpmf, line 323, characters 11-19


Source code
muSource code ESource code := fine (\esum_(xSource code in [set: T]) ((E x)%:R * mu x)%:E).

Local Notation "\P_[ mu ] E"Source code     := (pmf_prob mu E).

Definition pmf_cprob

pmf_cprob : forall {R : realType} [T : choiceType], SubPmf.type R T -> pred T -> pred T -> R pmf_cprob is not universe polymorphic Arguments pmf_cprob {R} [T] mu E A pmf_cprob is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_cprob Declared in library mathcomp.analysis.probability_theory.subpmf, line 327, characters 11-20


Source code
muSource code ESource code ASource code := \P_[mu] [predI E & A] / \P_[mu] A.

Local Notation "\P_[ mu , A ] E"Source code := (pmf_cprob mu A E).

Definition pmf_expectation

pmf_expectation : forall {R : realType} [T : choiceType], SubPmf.type R T -> (T -> R) -> R pmf_expectation is not universe polymorphic Arguments pmf_expectation {R} [T] mu f%_function_scope pmf_expectation is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_expectation Declared in library mathcomp.analysis.probability_theory.subpmf, line 331, characters 11-26


Source code
muSource code fSource code := fine (\esum_(xSource code in [set: T]) (f x * mu x)%:E).

Definition pmf_cexpectation

pmf_cexpectation : forall {R : realType} [T : choiceType], SubPmf.type R T -> (T -> R) -> pred T -> R pmf_cexpectation is not universe polymorphic Arguments pmf_cexpectation {R} [T] mu f%_function_scope A pmf_cexpectation is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_cexpectation Declared in library mathcomp.analysis.probability_theory.subpmf, line 333, characters 11-27


Source code
muSource code fSource code ASource code :=
  fine (\esum_(xSource code in [set: T]) (f x * \P_[mu, pred1 x] A)%:E).

Definition pmf_esummable

pmf_esummable : forall {R : realType} [T : choiceType], SubPmf.type R T -> (T -> R) -> bool pmf_esummable is not universe polymorphic Arguments pmf_esummable {R} [T] mu f%_function_scope pmf_esummable is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_esummable Declared in library mathcomp.analysis.probability_theory.subpmf, line 336, characters 11-24


Source code
muSource code fSource code := esummable [set: T] (EFin \o (f \* mu)).

End StdDefs.

Notation "\P_[ mu ] E"Source code     := (pmf_prob mu E) : subpmf_scope.
Notation "\P_[ mu , A ] E"Source code := (pmf_cprob mu E A) : subpmf_scope.
Notation "\E_[ mu ] f"Source code     := (pmf_expectation mu f) : subpmf_scope.
Notation "\E_[ mu , A ] f"Source code := (pmf_cexpectation mu f A) : subpmf_scope.
Notation dweightSource code mu := (\P_[mu] predT).

Definition subpmf_sum

subpmf_sum : forall {R : realType} {T : choiceType}, (T -> R) -> Prop subpmf_sum is not universe polymorphic Arguments subpmf_sum {R T} mu%_function_scope subpmf_sum is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.subpmf_sum Declared in library mathcomp.analysis.probability_theory.subpmf, line 346, characters 11-21


Source code
{RSource code : realType} {TSource code : choiceType} (muSource code : T -> R) :=
  (forall xSource code, 0 <= mu x) /\ (forall JSource code, uniq J -> \sum_(jSource code <- J) mu j <= 1).

Lemma subpmf_sumPSource code {RSource code : realType} {ISource code : finType} (muSource code : I -> R) :
  subpmf_sum mu <-> (forall xSource code, 0 <= mu x) /\ (\sum_jSource code mu j <= 1).
Proof.
split=> -[ ge0_mu le1]; split=> //.
+ by apply/le1; rewrite /index_enum -enumT enum_uniq.
+ move=> J uqJ; rewrite big_uniq 1?(le_trans _ le1) //=.
  by rewrite [X in _<=X](bigID (mem J)) /= lerDl sumr_ge0.
Qed.

Section pmf_zero.
Context {RSource code : realType} {TSource code : choiceType}.

Local Definition pmf_zero'

pmf_zero' not a defined object.


Source code
 (xSource code : T) : R := 0.

Let subpmf_zeroSource code : subpmf_sum pmf_zero'.
Proof.
by split=> // J _; rewrite big1 ?ler01. Qed.

HBSource code.instanceSource code DefinitionSource code _Source code :=Source code
  @uniq_isSubPmfSource code.Build R T pmf_zero' subpmf_zero.1 subpmf_zero.2.

End pmf_zero.

Definition pmf_zero

pmf_zero : forall {R : realType} {T : choiceType}, SubPmf.type R T pmf_zero is not universe polymorphic Arguments pmf_zero {R T} pmf_zero is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_zero Declared in library mathcomp.analysis.probability_theory.subpmf, line 371, characters 11-19


Source code
{RSource code : realType} {TSource code : choiceType} : R.-pmf T :=
  @pmf_zero' R T.

Lemma pmf_zeroESource code {RSource code : realType} {TSource code : choiceType} (xSource code : T) : pmf_zero x = 0 :> R.
Proof.
by []. Qed.

Lemma lef_pmf_zeroSource code {RSource code : realType} {TSource code : choiceType} (muSource code : R.-pmf T) :
  pmf_zero <=1 mu.
Proof.
by move=> x; rewrite pmf_zeroE. Qed.

Lemma measure_of_pmf_zeroSource code {RSource code : realType} {TSource code : choiceType}
    (SSource code : set (discrete_measurable_space T)) :
  measure_of_pmf pmf_zero S = mzero S :> \bar R.
Proof.
by rewrite /measure_of_pmf esum1. Qed.

Section Restr.
Context {RSource code : realType} {TSource code : choiceType} (pSource code : pred T).

Definition pmf_restr'

pmf_restr' : forall {R : realType} {T : choiceType}, pred T -> SubPmf.type R T -> T -> R pmf_restr' is not universe polymorphic Arguments pmf_restr' {R T} p mu x pmf_restr' is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_restr' Declared in library mathcomp.analysis.probability_theory.subpmf, line 389, characters 11-21


Source code
 (muSource code : R.-pmf T) := fun xSource code => if p x then mu x else 0.

Let subpmf_restrSource code (muSource code : R.-pmf T) : subpmf_sum (pmf_restr' mu).
Proof.
split=> [x|J]; first by rewrite /pmf_restr'; case: ifP.
move=> eqJ; apply/(@le_trans _ _ (\sum_(jSource code <- J) `|mu j|)).
- apply/ler_sum=> i _; rewrite /pmf_restr'; case: ifPn => _ //.
  exact: ler_norm.
- rewrite -lee_fin (le_trans _ (subpmf_sum_le1 mu))//.
  rewrite (@eq_esum _ _ _ _ (fun ySource code => `|(EFin \o mu) y|%E))//=.
    by move=> *; rewrite ger0_norm.
  exact: sum_esum_ge.
Qed.

HBSource code.instanceSource code DefinitionSource code _Source code (muSource code :Source code RSource code.-pmf T) := @uniq_isSubPmf.Build R T
  (pmf_restr' mu) (subpmf_restr mu).1 (subpmf_restr mu).2.

Definition pmf_restr

pmf_restr : forall {R : realType} {T : choiceType}, pred T -> SubPmf.type R T -> SubPmf.type R T pmf_restr is not universe polymorphic Arguments pmf_restr {R T} p _ pmf_restr is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_restr Declared in library mathcomp.analysis.probability_theory.subpmf, line 406, characters 11-20


Source code
: R .-pmf T -> R .-pmf T := pmf_restr'.

End Restr.

Lemma pmf_restrESource code {RSource code : realType} {TSource code : choiceType} pSource code muSource code {xSource code:T} :
  pmf_restr p mu x = if p x then mu x else 0 :>R.
Proof.
by []. Qed.

Lemma measure_of_pmf_restrSource code {RSource code : realType} {TSource code : choiceType} (pSource code : pred T)
    (muSource code : R.-pmf T)
    (mpSource code : measurable ([set` p] : set (discrete_measurable_space T)))
    (SSource code : set (discrete_measurable_space T)) :
  measure_of_pmf (pmf_restr p mu) S = mrestr (measure_of_pmf mu) mp S.
Proof.
rewrite /mrestr /measure_of_pmf esum_mkcondr; apply: eq_esum => x _ /=.
by rewrite /pmf_restr mem_setE unfold_in (fun_if EFin).
Qed.

Section pr_pmf.
Import MeasurableR.
Context {dSource code} {TSource code : pmeasurableType d} {RSource code : realType} (PSource code : probability T R)
  (XSource code : {dRV P >-> R}).

Local Notation pmfXSource code := (@pmf _ _ _ P X).

Lemma esum_pmf_setTSource code :
  esum [set: R] (EFin \o pmfX) = \esum_(rSource code in range X) (pmfX r)%:E.
Proof.
rewrite (esumID (range X) [set: R] (EFin \o pmfX)).
  by move=> i _; rewrite lee_fin.
rewrite setTI [X in _ + X]esum1 ?adde0//= => r [_ /= nr].
by rewrite not_range_pmf0.
Qed.

Lemma esum_pmf_range1Source code : \esum_(rSource code in range X) (pmfX r)%:E = 1%E.
Proof.
rewrite (reindex_esum (dRV_dom X) (range X) (dRV_enum X))//.
transitivity (\esum_(kSource code in dRV_dom X) enum_prob X k).
  apply: eq_esum => k kd/=.
  by rewrite /enum_prob patchE mem_set// /pmf fineK// fin_num_measure.
rewrite -[X in \esum_(kSource code in X) _]set_mem_set -nneseries_esum.
  by move=> n _; rewrite /enum_prob patchE; case: ifP.
rewrite eseries_mkcond -[RHS](sum_enum_prob X measurable_set1).
apply: eq_eseriesr => k _; case: ifPn => // kd.
by rewrite /enum_prob patchE (negbTE kd).
Qed.

Lemma esum_pmf_predSource code (ASource code : set R) :
  esum [set: R] (EFin \o (fun rSource code => (r \in A)%:R * pmfX r))%R =
  \esum_(rSource code in A) (pmfX r)%:E.
Proof.
rewrite [RHS]esum_mkcond; apply: eq_esum => r _.
by case: (r \in A) => /=; rewrite ?mul1r ?mul0r.
Qed.

Lemma pmf_prob_dRVSource code (ASource code : set R) : measurable A ->
  (\P_[pmfX] (mem A))%:E = distribution P X A.
Proof.
move=> mA; rewrite /pmf_prob esum_pmf_pred dRV_esum_pmf_distribution// fineK//.
by rewrite fin_num_measure.
Qed.

End pr_pmf.

Section RestrTheory.
Context {RSource code : realType} {TSource code : choiceType}.

Lemma pmf_restrDSource code (muSource code : R.-pmf T) (pSource code : pred T) xSource code :
  mu x = pmf_restr p mu x + pmf_restr (predC p) mu x.
Proof.
by rewrite 2!pmf_restrE !inE; case: ifPn; rewrite (addr0, add0r). Qed.

Lemma supp_pmf_restrSource code (muSource code : R.-pmf T) (pSource code : pred T) xSource code :
  (x \in supp (pmf_restr p mu)) = (x \in supp mu) && p x.
Proof.
apply/mem_suppP/idP.
- by rewrite pmf_restrE; case: ifP=> // _ /mem_suppP ->.
- by case/andP; rewrite pmf_restrE => /mem_suppP ? ->.
Qed.

End RestrTheory.

Section DCount.
Context {RSource code : realType} {TSource code : choiceType}.
Implicit Types (s : seq T).

Local Definition pmf_count'

pmf_count' not a defined object.


Source code
 (sSource code : seq T) : T -> R :=
  fun xSource code : T => (count (pred1 x) s)%:R / (size s)%:R.

Local Lemma pmf_count_ge0Source code sSource code xSource code : 0 <= pmf_count' s x.
Proof.
by rewrite mulr_ge0 ?invr_ge0 // ler0n. Qed.

Local Lemma pmf_count_sum_le1Source code sSource code JSource code : uniq J -> \sum_(iSource code <- J) pmf_count' s i <= 1.
Proof.
move=> uqJ; rewrite -mulr_suml /= -natr_sum; case: (size s =P 0%N).
  by move=> ->; rewrite invr0 mulr0 ler01.
move=> /eqP nz_s; rewrite ler_pdivrMr ?ltr0n ?lt0n // mul1r.
rewrite ler_nat (bigID (mem s)) /= [X in (_+X)%N]big1 ?addn0.
   by move=> i /count_memPn.
have -> : (size s = \sum_(iSource code <- undup s) count_mem i s)%N.
  rewrite -sum1_size -big_undup_iterop_count; apply: eq_bigr => i _.
  by rewrite Monoid.iteropE iter_addn addn0 mul1n.
rewrite [X in (_ <= X)%N](bigID (mem J)) /= -ltnS -addSn.
rewrite ltn_addr //= ltnS -big_filter -[X in (_ <= X)%N]big_filter.
rewrite leq_eqVlt; apply/orP; left; apply/eqP/perm_big.
apply/uniq_perm; rewrite ?filter_uniq ?undup_uniq//.
by move=> x; rewrite !mem_filter mem_undup andbC.
Qed.

Let subpmf_countSource code sSource code : subpmf_sum (pmf_count' s).
Proof.
by split; [exact/pmf_count_ge0|exact/pmf_count_sum_le1]. Qed.

HBSource code.instanceSource code DefinitionSource code _Source code (sSource code :Source code seqSource code T) :=
  @uniq_isSubPmf.Build R T (pmf_count' s) (subpmf_count s).1 (subpmf_count s).2.

Definition pmf_count

pmf_count : forall {R : realType} {T : choiceType}, seq T -> SubPmf.type R T pmf_count is not universe polymorphic Arguments pmf_count {R T} s%_seq_scope pmf_count is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_count Declared in library mathcomp.analysis.probability_theory.subpmf, line 520, characters 11-20


Source code
sSource code : R.-pmf T := pmf_count' s.

Lemma pmf_countESource code sSource code xSource code : pmf_count s x = (count (pred1 x) s)%:R / (size s)%:R.
Proof.
by []. Qed.

Lemma pmf_count_sum_eq1Source code sSource code : (0 < size s)%N ->
  \sum_(iSource code <- undup s) pmf_count s i = 1.
Proof.
move=> gt0_s; rewrite -mulr_suml -natr_sum.
apply/(mulIf (x := (size s)%:R)); first by rewrite pnatr_eq0 -lt0n.
rewrite mul1r divfK ?pnatr_eq0 -?lt0n//; congr (_%:R).
rewrite -sum1_size -[in RHS]big_undup_iterop_count/=; apply: eq_bigr => i _.
by rewrite Monoid.iteropE iter_addn addn0 mul1n.
Qed.

Lemma esummable_pmf_countSource code sSource code : esummable [set :T] (EFin \o (pmf_count s)).
Proof.
rewrite /esummable (@le_lt_trans _ _ 1%:E) ?ltey// ge0_esum.
  by move => *; rewrite lee_fin.
rewrite ge_ereal_sup//= => _ [X [finX _]] <-.
rewrite fsumEFin // lee_fin fsbig_finite //=.
rewrite (eq_bigr (pmf_count s)).
  by move => ? ?; rewrite ger0_norm ?pmf_count_ge0.
exact/pmf_count_sum_le1.
Qed.

End DCount.

HB

pmf_ret_unlock_subterm : unlockable (fun (R : realType) (T : choiceType) (x : T) => pmf_count [:: x]) pmf_ret_unlock_subterm is not universe polymorphic pmf_ret_unlock_subterm is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_ret_unlock_subterm Declared in library mathcomp.analysis.probability_theory.subpmf, line 548, characters 0-100


Source code
.lock

pmf_ret_unlock_subterm : unlockable (fun (R : realType) (T : choiceType) (x : T) => pmf_count [:: x]) pmf_ret_unlock_subterm is not universe polymorphic pmf_ret_unlock_subterm is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_ret_unlock_subterm Declared in library mathcomp.analysis.probability_theory.subpmf, line 548, characters 0-100


Source code
Definition

pmf_ret_unlock_subterm : unlockable (fun (R : realType) (T : choiceType) (x : T) => pmf_count [:: x]) pmf_ret_unlock_subterm is not universe polymorphic pmf_ret_unlock_subterm is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_ret_unlock_subterm Declared in library mathcomp.analysis.probability_theory.subpmf, line 548, characters 0-100


Source code
pmf_ret

pmf_ret_unlock_subterm : unlockable (fun (R : realType) (T : choiceType) (x : T) => pmf_count [:: x]) pmf_ret_unlock_subterm is not universe polymorphic pmf_ret_unlock_subterm is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_ret_unlock_subterm Declared in library mathcomp.analysis.probability_theory.subpmf, line 548, characters 0-100


Source code
{R

pmf_ret_unlock_subterm : unlockable (fun (R : realType) (T : choiceType) (x : T) => pmf_count [:: x]) pmf_ret_unlock_subterm is not universe polymorphic pmf_ret_unlock_subterm is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_ret_unlock_subterm Declared in library mathcomp.analysis.probability_theory.subpmf, line 548, characters 0-100


Source code
: realType} {TSource code : choiceType} (xSource code : T) : R.-pmf T :=
  pmf_count [:: x].
Canonical locked_pmf_ret

locked_pmf_ret : unlockable (fun (R : realType) (T : choiceType) (x : T) => pmf_count [:: x]) locked_pmf_ret is not universe polymorphic locked_pmf_ret is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.locked_pmf_ret Declared in library mathcomp.analysis.probability_theory.subpmf, line 550, characters 10-24


Source code
:= Unlockable pmf_ret.unlock.

Section pmf_ret.
Context {RSource code : realType} {TSource code : choiceType}.
Implicit Types (x y : T) (s : seq T).

Lemma pmf_retESource code xSource code ySource code : pmf_ret x y = (x == y)%:R :> R.
Proof.
by rewrite unlock pmf_countE/= addn0 divr1. Qed.

Lemma pmf_retxxSource code xSource code : pmf_ret x x = 1 :> R.
Proof.
by rewrite pmf_retE eqxx. Qed.

Lemma pmf_ret0Source code xSource code ySource code : x != y -> (pmf_ret x) y = 0 :> R.
Proof.
by rewrite unlock pmf_countE /= addn0 divr1 => /negPf ->. Qed.

Lemma in_pmf_retSource code (tSource code t'Source code : T) : t' \in @supp R _ (pmf_ret t) -> t' = t :> T.
Proof.

End pmf_ret.

Lemma measure_of_pmf_retSource code {RSource code : realType} {TSource code : choiceType} (xSource code : T)
    (SSource code : set (discrete_measurable_space T)) :
  measure_of_pmf (pmf_ret x) S = \d_(x : discrete_measurable_space T) S :> \bar R.
Proof.
rewrite /measure_of_pmf diracE esum_mkcond esum_setT_discrete.
rewrite (@eq_esum R T _ _ (fun ySource code => if x == y then (y \in S)%:R%:E else 0%E)).
  by move=> y _ /=; rewrite pmf_retE; case: ifPn => yS //; case: ifPn.
by rewrite esum_if_eq_op_set1.
Qed.

Definition pmf_uniform

pmf_uniform : forall {R : realType} {T : choiceType}, seq T -> SubPmf.type R T pmf_uniform is not universe polymorphic Arguments pmf_uniform {R T} s%_seq_scope pmf_uniform is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_uniform Declared in library mathcomp.analysis.probability_theory.subpmf, line 583, characters 11-22


Source code
{RSource code : realType} {TSource code : choiceType} (sSource code : seq T) : R.-pmf T :=
  pmf_count (undup s).

Lemma pmf_uniformESource code {RSource code : realType} {TSource code : choiceType} (sSource code : seq T) xSource code :
  (pmf_uniform s) x = (x \in s)%:R / (size (undup s))%:R :> R.
Proof.

Section Flip.
Context {RSource code : realType}.

Local Notation clampSource code := (@clamp R 0 1).

Definition pmf_flip'

pmf_flip' : forall {R : realType}, R -> bool -> R pmf_flip' is not universe polymorphic Arguments pmf_flip' {R} xt%_ring_scope b%_bool_scope pmf_flip' is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_flip' Declared in library mathcomp.analysis.probability_theory.subpmf, line 597, characters 11-20


Source code
 (xtSource code : R) := fun bSource code => if b then clamp xt else 1 - clamp xt.

Let subpmf_flipSource code xtSource code : subpmf_sum (pmf_flip' xt).
Proof.
apply/subpmf_sumP; split=> [b|].
  by case: b; rewrite ?subr_ge0 clamp_gele.
by rewrite /index_enum !unlock /= addr0 addrC subrK.
Qed.

HBSource code.instanceSource code DefinitionSource code _Source code (xtSource code :Source code RSource code) :=
  @uniq_isSubPmf.Build R _ (pmf_flip' xt) (subpmf_flip xt).1 (subpmf_flip xt).2.

Definition pmf_flip

pmf_flip : forall {R : realType}, R -> SubPmf.type R bool pmf_flip is not universe polymorphic Arguments pmf_flip {R} xt%_ring_scope pmf_flip is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_flip Declared in library mathcomp.analysis.probability_theory.subpmf, line 609, characters 11-19


Source code
xtSource code : R.-pmf bool := pmf_flip' xt.

Lemma pmf_flipESource code xtSource code bSource code : pmf_flip xt b = if b then clamp xt else 1 - clamp xt.
Proof.
by []. Qed.

End Flip.

Section Bind.
Context {RSource code : realType} {TSource code USource code : choiceType} (fSource code : T -> R.-pmf U) (muSource code : R.-pmf T).

Local Definition pmf_bind'

pmf_bind' not a defined object.


Source code
 (ySource code : U) :=
  fine (\esum_(xSource code in [set: T]) (mu x * f x y)%:E).

Let pmf_bind_ge0Source code xSource code : 0 <= pmf_bind' x.
Proof.
by rewrite fine_ge0// esum_ge0//= => t _; rewrite lee_fin mulr_ge0.
Qed.

Let subpmf_bindSource code : subpmf_sum pmf_bind'.
Proof.
split=> [x|J uqJ]; first exact: pmf_bind_ge0.
rewrite /pmf_bind' -lee_fin.
have hpos x i : (0 <= (mu x * f x i)%:E)%E.
  by rewrite EFinM mule_ge0//= lee_fin.
apply: le_trans; first exact: sum_esum_ge.
apply/(le_trans _ (subpmf_sum_le1 mu)).
apply/(le_trans (le_esum_fine _ _ hpos)).
rewrite exchange_esum// le_esum// => i _.
under eq_esum do rewrite EFinM.
rewrite esumZ ?lee_fin// => [?|]; first by rewrite lee_fin.
rewrite muleC gee_pMl ?lee_fin//; last exact: subpmf_sum_le1.
rewrite (eq_esum _ _ (fun xSource code  => `|(EFin \o f i) x|%E)).
  by move=> *; rewrite gee0_abs// lee_fin.
by rewrite -esummableE.
Qed.

HBSource code.instanceSource code DefinitionSource code _Source code :=Source code
  @uniq_isSubPmfSource code.Build R U pmf_bind' subpmf_bind.1 subpmf_bind.2.

Definition pmf_bind

pmf_bind : forall {R : realType} {T U : choiceType}, (T -> SubPmf.type R U) -> SubPmf.type R T -> SubPmf.type R U pmf_bind is not universe polymorphic Arguments pmf_bind {R T U} f%_function_scope mu pmf_bind is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_bind Declared in library mathcomp.analysis.probability_theory.subpmf, line 648, characters 11-19


Source code
: R.-pmf U := pmf_bind'.

End Bind.

Notation "\bind_ ( i <- d ) E"Source code := (pmf_bind (fun iSource code => E) d) : subpmf_scope.

Lemma pmf_bindESource code {RSource code : realType} {TSource code USource code : choiceType} (fSource code : T -> R.-pmf U)
    (muSource code : R.-pmf T) (ySource code : U) :
  (\bind_( iSource code <- mu) f i) y = fine (\esum_(xSource code in [set: T]) (mu x * f x y)%:E).
Proof.
by []. Qed.

Section BindTheory.
Context {RSource code : realType} {TSource code USource code : choiceType}.
Implicit Types (f g : T -> R.-pmf U) (mu nu : R.-pmf T).

Lemma pmf_bind_zeroSource code fSource code : \bind_(xSource code <- pmf_zero) f x =1 pmf_zero.
Proof.
by move=> x; rewrite pmf_bindE esum1// => *; rewrite /= EFinM mul0e. Qed.

Lemma pmf_ret_bindSource code fSource code vSource code : \bind_(ySource code <- pmf_ret v) f y =1 f v.
Proof.
move=> y; rewrite pmf_bindE (esumID [set v]).
  by move=> *; rewrite lee_fin mulr_ge0.
rewrite setI1//= in_setT esum_set1 esum1/=.
  by move => i [? /eqP h]; rewrite pmf_ret0 ?mul0r//= eq_sym.
by rewrite pmf_retxx addr0 mul1r.
Qed.

Lemma pmf_bind_retSource code muSource code : \bind_(tSource code <- mu) (pmf_ret t) =1 mu.
Proof.
move=> v; rewrite pmf_bindE.
rewrite (esumID ([set v]%classic)).
  by move=> ? ; rewrite lee_fin mulr_ge0.
rewrite setI1 in_setT esum_set1 esum1/=.
  by move => i [? /eqP h]; rewrite pmf_ret0 ?mulr0.
by rewrite pmf_retxx mulr1 addr0.
Qed.

Lemma supp_eq_pmf_bindSource code fSource code gSource code muSource code nuSource code : {in supp mu, f =2 g} -> mu =1 nu ->
  pmf_bind f mu =1 pmf_bind g nu.
Proof.
move=> eq_f eq_mu y; rewrite /pmf_bind.
rewrite [X in fine X = _](_ : _ = \esum_(xSource code in [set: T]) (nu x * g x y)%:E)//.
apply/eq_esum => x _; rewrite -eq_mu !EFinM.
case/boolP : (x \in supp mu) => [/eq_f ->//|].
by move/memNsuppP => ->; rewrite !mul0e.
Qed.

Lemma esummable_pmf_bind_eltSource code (fSource code : T -> R.-pmf U) (muSource code : R.-pmf T) ySource code :
  esummable [set: T] (fun xSource code => (mu x * f x y)%:E).
Proof.
rewrite /esummable.
under eq_esum do rewrite EFinM.
apply: esummableMr => //.
  exists 1%E => // => i _.
  by rewrite gee0_abs lee_fin// subpmf_le1.
exact: subpmf_summable.
Qed.

Lemma pmf_bind_is_fin_numSource code (fSource code : T -> R.-pmf U) (muSource code : R.-pmf T) ySource code :
  \esum_(xSource code in [set: T]) (mu x * f x y)%:E \is a fin_num.
Proof.
rewrite esum_abse.
  by move => ?; rewrite mulr_ge0.
by rewrite -esummableE; exact: esummable_pmf_bind_elt.
Qed.

Lemma EFin_pmf_bindSource code (fSource code : T -> R.-pmf U) (muSource code : R.-pmf T) ySource code :
  (pmf_bind f mu y)%:E = \esum_(xSource code in [set: T]) (mu x * f x y)%:E.
Proof.
by rewrite /pmf_bind fineK// ?pmf_bind_is_fin_num. Qed.

Lemma supp_le_pmf_bindSource code fSource code gSource code muSource code : {in supp mu, f <=2 g} ->
  \bind_(iSource code <- mu) f i <=1 \bind_(iSource code <- mu) g i.
Proof.
move=> le_f y; rewrite /pmf_bind fine_le ?pmf_bind_is_fin_num//.
apply: le_esum => x ?.
case/boolP : (x \in supp mu) => [/le_f ?|].
rewrite !EFinM lee_pmul //= lee_tofin //=.
by move/memNsuppP => ->; rewrite !EFinM !mul0e.
Qed.

Lemma le_pmf_bindSource code fSource code muSource code nuSource code : mu <=1 nu ->
  \bind_(iSource code <- mu) f i <=1 \bind_(iSource code <- nu) f i.
Proof.
move=> le_mu y; rewrite !/pmf_bind fine_le ?pmf_bind_is_fin_num//.
apply: le_esum => x ?.
by rewrite !EFinM lee_pmul //= lee_tofin.
Qed.

Lemma supp_le_pmf_bind2Source code fSource code gSource code muSource code nuSource code : mu <=1 nu ->
  {in supp mu, forall xSource code, f x <=1 g x} ->
  \bind_(xSource code <- mu) f x <=1 \bind_(xSource code <- nu) g x.
Proof.
move=> le_mu le_fg x.
exact/(le_trans (supp_le_pmf_bind le_fg _))/le_pmf_bind.
Qed.

Lemma pmf_bindCSource code (muSource code : R.-pmf T) (nuSource code : R.-pmf U) ySource code :
  (\bind_(_ <- mu) nu) y = dweight mu * nu y.
Proof.
rewrite pmf_bindE.
under eq_esum do rewrite mulrC EFinM.
rewrite esumZ; first by move=> *; rewrite lee_fin.
rewrite fineM// [in LHS]mulrC [in RHS]/pmf_prob.
by under [in RHS]eq_esum do rewrite mul1r.
Qed.

Lemma supp_pmf_bindSource code fSource code muSource code ySource code : y \in supp (\bind_(xSource code <- mu) f x) ->
  exists2 xSource code, x \in supp mu & f x y != 0.
Proof.
rewrite mem_supp pmf_bindE => nz_pmf_bind.
have /esum_neq0[x _] : \esum_(xSource code in [set: T]) (mu x * f x y)%:E != 0.
  by apply: contra nz_pmf_bind => /eqP ->; rewrite fine0.
rewrite eqe mulf_eq0 negb_or => /andP[nz_mu nz_f].
by exists x.
Qed.

Let esum_eq0_pmf_bind_eltSource code (fSource code : T -> R.-pmf U) (muSource code : R.-pmf T) xSource code ySource code:
  fine (\esum_(xSource code in [set: T]) (mu x * f x y)%:E) = 0 ->
  (mu x * f x y)%:E = 0.
Proof.
move=> h0.
apply /(@esum_eq0P _ _  [set: T] (fun iSource code => (mu i * f i y)%:E)) => //.
+ by move=> i _; rewrite lee_fin mulr_ge0.
+ by rewrite -(fineK (pmf_bind_is_fin_num f mu y)) h0.
Qed.

Lemma pmf_bind_mem_suppSource code fSource code muSource code xSource code ySource code :
  x \in supp mu -> f x y != 0 -> y \in supp (pmf_bind f mu).
Proof.
move=> /mem_suppP /eqP mux nz_fxy.
apply/memNsuppP => /(esum_eq0_pmf_bind_elt x).
by move=> /eqP; rewrite eqe mulf_eq0 (negbTE nz_fxy) orbF; exact/negP.
Qed.

Lemma pmf_bind_eq0Source code (fSource code : T -> U) muSource code ySource code :
  {in supp mu, forall xSource code, f x != y} -> (\bind_(xSource code <- mu) pmf_ret (f x)) y = 0.
Proof.
move=> h; rewrite pmf_bindE esum1// => x _.
case/boolP : (x \in supp mu) => [/h|/memNsuppP ->].
- by rewrite pmf_retE => /negbTE ->; rewrite mulr0.
- by rewrite mul0r.
Qed.

Lemma eq0_pmf_bindSource code (muSource code : R.-pmf T) (FSource code : T -> R.-pmf U ) ySource code :
  (\bind_(xSource code <- mu) F x) y = 0 -> forall xSource code, x \in supp mu -> F x y = 0.
Proof.
move=> /esum_eq0_pmf_bind_elt h x /mem_suppP /eqP mu_x.
by move/eqP : (h x); rewrite eqe mulf_eq0 (negbTE mu_x) /= => /eqP.
Qed.

End BindTheory.

Definition pmf_bindr

pmf_bindr : forall {R : realType} {T : choiceType}, (T -> SubPmf.type R T) -> SubPmf.type R T -> SubPmf.type R T pmf_bindr is not universe polymorphic Arguments pmf_bindr {R T} f%_function_scope d pmf_bindr is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_bindr Declared in library mathcomp.analysis.probability_theory.subpmf, line 800, characters 11-20


Source code
{RSource code : realType} {TSource code : choiceType} (fSource code : T -> R.-pmf T) :=
  fun dSource code => \bind_(xSource code <- d) f x.

Definition pmf_iter

pmf_iter : forall {R : realType} {T : choiceType}, nat -> (T -> SubPmf.type R T) -> T -> SubPmf.type R T pmf_iter is not universe polymorphic Arguments pmf_iter {R T} n%_nat_scope f%_function_scope a pmf_iter is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_iter Declared in library mathcomp.analysis.probability_theory.subpmf, line 803, characters 11-19


Source code
{RSource code : realType} {TSource code : choiceType} nSource code (fSource code : T -> R.-pmf T) :=
  fun aSource code => iter n (pmf_bindr f) (pmf_ret a).

Section MeasureOfPmf_Bind.
Context {RSource code : realType} {TSource code USource code : choiceType}.

Local Open Scope ereal_scope.

Lemma measure_of_pmf_bindSource code (muSource code : R.-pmf T) (nuSource code : T -> R.-pmf U)
    (SSource code : set (discrete_measurable_space U)) :
  measure_of_pmf (pmf_bind nu mu) S =
  \int[measure_of_pmf mu]_xSource code measure_of_pmf (nu x) S.
Proof.
rewrite integral_measure_of_pmf// /measure_of_pmf.
under eq_esum do rewrite /= EFin_pmf_bind.
rewrite exchange_esum.
  by move=> i j; rewrite lee_fin mulr_ge0.
apply: eq_esum => x _.
by rewrite muleC -esumZ //= => y _; rewrite lee_fin.
Qed.

Lemma measurable_measure_of_pmfSource code (nuSource code : T -> R.-pmf U) :
  measurable_fun [set: discrete_measurable_space T]
    (fun xSource code => (measure_of_pmf (nu x) : giry (discrete_measurable_space U) R)).
Proof.
by []. Qed.

Lemma measure_of_pmf_bind_girySource code (muSource code : R.-pmf T) (nuSource code : T -> R.-pmf U)
    (SSource code : set (discrete_measurable_space U)) :
  measure_of_pmf (pmf_bind nu mu) S =
  giry_bind (measure_of_pmf mu) (measurable_measure_of_pmf nu) S.
Proof.
rewrite /giry_bind/= giry_int_map//=.
  exact: measurable_giry_ev.
exact: measure_of_pmf_bind.
Qed.

End MeasureOfPmf_Bind.

Section Pmf_BindPmf_Bind.
Context {RSource code : realType} {TSource code USource code VSource code : choiceType}
  (f1Source code : T -> R.-pmf U) (f2Source code : U -> R.-pmf V).

Lemma pmf_bind_pmf_bindSource code (muSource code : R.-pmf T) :
  \bind_(xSource code <- \bind_(ySource code <- mu) f1 y) f2 x =1
  \bind_(ySource code <- mu) (\bind_(xSource code <- f1 y) f2 x).
Proof.
move=> z; rewrite 2!pmf_bindE.
pose S ySource code xSource code := mu x * f1 x y * f2 y z.
rewrite (eq_esum _ _ (fun ySource code => \esum_(xSource code in setT) (S y x)%:E)).
  move=> y _; rewrite EFinM EFin_pmf_bind//.
  rewrite muleC -esumZ ?lee_fin// => [? _|].
    by rewrite EFinM lee_fin mulr_ge0.
  by apply eq_esum => *; rewrite -EFinM mulrC.
rewrite (eq_esum _ _ (fun xSource code => \esum_(ySource code in setT) (S y x)%:E)).
  move=> x _; rewrite EFinM EFin_pmf_bind//.
  rewrite -esumZ ?lee_fin// => [? _|].
    by rewrite EFinM lee_fin mulr_ge0.
  by apply eq_esum => * ; rewrite -EFinM mulrA.
by rewrite exchange_esum// => ? ?; rewrite /S EFinM lee_fin ?mulr_ge0.
Qed.

End Pmf_BindPmf_Bind.

Section Pmf_BindAlg.
Context {RSource code : realType} {TSource code USource code : choiceType} (muSource code mu1Source code mu2Source code : R.-pmf T).

Lemma pmf_bind_additiveSource code (fSource code : T -> R.-pmf U) zSource code :
  (forall xSource code, mu x = mu1 x + mu2 x) ->
  (\bind_(xSource code <- mu) f x) z = (\bind_(xSource code <- mu1) f x) z + (\bind_(xSource code <- mu2) f x) z.
Proof.
move=> muD; rewrite pmf_bindE.
under eq_esum do rewrite muD mulrDl EFinD.
rewrite esumD; [by move=> ? ?; rewrite /S EFinM lee_tofin ?mulr_ge0..|].
by rewrite fineD//= pmf_bind_is_fin_num.
Qed.

End Pmf_BindAlg.

Section pmf_lim.
Context {RSource code : realType} (TSource code : choiceType).
Implicit Types (f g : nat -> R.-pmf T).

Import numFieldNormedType.Exports.

Local Definition pmf_lim'

pmf_lim' not a defined object.


Source code
fSource code : T -> R :=
  fun xSource code => fine (limn_einf (fun nSource code => (f n x)%:E)).

Local Lemma pmf_lim_ge0'Source code fSource code xSource code : (0 <= limn_einf (fun nSource code => (f n x)%:E))%E.
Proof.
rewrite limn_einf_lim; apply: lime_ge; first exact: is_cvg_einfs.
apply: nearW => n; apply: le_ereal_inf => _ [m _] <-.
by rewrite lee_fin.
Qed.

Local Lemma pmf_lim_le1'Source code fSource code xSource code : (limn_einf (fun nSource code => (f n x)%:E) <= 1%:E)%E.
Proof.
rewrite limn_einf_lim; apply: lime_le; first exact: is_cvg_einfs.
apply: nearW => n; apply: (@le_trans _ _ (f n x)%:E); first exact: ge_einfs.
by rewrite lee_fin subpmf_le1.
Qed.

Let limn_einf_fin_numSource code fSource code xSource code : limn_einf (fun nSource code => (f n x)%:E) \is a fin_num.
Proof.
have h0 : (0 <= limn_einf (fun nSource code => (f n x)%:E))%E by exact: pmf_lim_ge0'.
rewrite ge0_fin_numE//.
by apply: le_lt_trans; [exact: pmf_lim_le1' |exact: ltry].
Qed.

Local Lemma EFin_pmf_lim'Source code fSource code xSource code : (pmf_lim' f x)%:E = limn_einf (fun nSource code => (f n x)%:E).
Proof.
by rewrite /lim fineK// fin_liminf. Qed.

Let subpmf_limSource code fSource code : subpmf_sum (pmf_lim' f).
Proof.
split=> [x|J uqJ]; first by rewrite /lim; exact/fine_ge0/pmf_lim_ge0'.
rewrite -lee_fin -sumEFin.
under eq_bigr => j _ do rewrite EFin_pmf_lim' limn_einf_lim.
have ndE j : nondecreasing_seq (einfs (fun nSource code => (f n j)%:E)).
  exact: nondecreasing_einfs.
have finE j : limn (einfs (fun nSource code => (f n j)%:E)) \is a fin_num.
  by rewrite -limn_einf_lim limn_einf_fin_num.
rewrite (ereal_nondecreasing_sum_limn ndE finE); apply: lime_le.
  apply: ereal_nondecreasing_is_cvgn => n m nm.
  by apply: lee_sum => j _; exact: ndE.
apply: nearW => n; apply: (@le_trans _ _ (\sum_(jSource code <- J) (f n j)%:E)).
  by apply: lee_sum => j _; exact: ge_einfs.
rewrite sumEFin; apply: le_trans (subpmf_sum_le1 (f n)).
exact: sum_esum_ge.
Qed.

HBSource code.instanceSource code DefinitionSource code _Source code fSource code :=Source code
  @uniq_isSubPmfSource code.Build R T (pmf_lim' f) (subpmf_lim f).1 (subpmf_lim f).2.

Definition pmf_lim

pmf_lim : forall {R : realType} [T : choiceType], (nat -> SubPmf.type R T) -> SubPmf.type R T pmf_lim is not universe polymorphic Arguments pmf_lim {R} [T] f%_function_scope pmf_lim is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_lim Declared in library mathcomp.analysis.probability_theory.subpmf, line 935, characters 11-18


Source code
fSource code : R.-pmf T := pmf_lim' f.

Lemma pmf_limESource code fSource code xSource code : pmf_lim f x = fine (limn_einf (fun nSource code => (f n x)%:E)).
Proof.
by []. Qed.

Lemma pmf_lim_limnSource code fSource code : nondecreasing_fun (f : _ -> _ -> R) ->
  forall xSource code, pmf_lim f x = fine (limn (fun nSource code => (f n x)%:E)).
Proof.
move=> ndf x; rewrite pmf_limE is_cvg_limn_einfE//=.
apply: ereal_nondecreasing_is_cvgn => m n mn.
by rewrite lee_fin; exact/lefP/ndf.
Qed.

Lemma EFin_pmf_limSource code fSource code xSource code : (pmf_lim f x)%:E = limn_einf (fun nSource code => (f n x)%:E).
Proof.
exact: EFin_pmf_lim'. Qed.

Lemma EFin_pmf_lim_limnSource code fSource code : nondecreasing_fun (f : _ -> _ -> R) ->
  forall xSource code, (pmf_lim f x)%:E = limn (fun nSource code => (f n x)%:E).
Proof.
move=> ndf x; rewrite EFin_pmf_lim is_cvg_limn_einfE//.
apply: ereal_nondecreasing_is_cvgn => m n mn.
by rewrite lee_fin; exact/lefP/ndf.
Qed.

Lemma EFin_pmf_lim_supSource code fSource code : nondecreasing_fun (f : _ -> _ -> R) ->
  forall xSource code, (pmf_lim f x)%:E = ereal_sup (range (fun nSource code => (f n x)%:E)).
Proof.
move=> ndf x.
rewrite (EFin_pmf_lim_limn ndf) -sup_range_lim// => m n mn.
by rewrite lee_fin; exact/lefP/ndf.
Qed.

End pmf_lim.

Notation "\lim_ ( n ) E"Source code := (pmf_lim (fun nSource code => E)) : subpmf_scope.

Section Pmf_LimTheory.
Context {RSource code : realType} {TSource code USource code : choiceType}.
Implicit Types (f g : nat -> R.-pmf T) (h : T -> R.-pmf U) (mu : R.-pmf T).

Lemma pmf_limCSource code muSource code : \lim_(nSource code) mu =1 mu.
Proof.
by move=> x; rewrite pmf_limE limn_einf_cst. Qed.

Lemma pmf_lim_bumpSource code fSource code : \lim_(nSource code) f n.+1 =1 pmf_lim f.
Proof.
by move=> x; rewrite pmf_limE (limn_einf_shiftS (fun nSource code => (f n x)%:E)).
Qed.

Lemma pmf_lim_liftSource code fSource code pSource code : \lim_(nSource code) f (n + p)%N =1 pmf_lim f.
Proof.
by move=> x; rewrite pmf_limE (limn_einf_shift_new (fun nSource code => (f n x)%:E) p).
Qed.

Lemma pmf_lim_ge0Source code fSource code xSource code : 0 <= pmf_lim f x.
Proof.
by []. Qed.

Lemma pmf_lim_le1Source code fSource code xSource code : pmf_lim f x <= 1.
Proof.
by rewrite -lee_fin EFin_pmf_lim; exact: pmf_lim_le1'. Qed.

Lemma le_pmf_limSource code fSource code gSource code : (forall nSource code, f n <=1 g n) -> pmf_lim f <=1 pmf_lim g.
Proof.
move=> le x; rewrite -lee_fin !EFin_pmf_lim !limn_einf_lim.
apply: lee_lim; [exact: is_cvg_einfs..|].
apply: nearW => n; apply: le_ereal_inf => _ [m /= nm] <-.
by apply: (@le_trans _ _ ((f m x)%:E)); [exact: ge_einfs|rewrite lee_fin].
Qed.

Lemma eq_pmf_limSource code fSource code gSource code : (forall nSource code, f n =1 g n) -> pmf_lim f =1 pmf_lim g.
Proof.
move=> eq_fg x.
apply/eqP; rewrite eq_le; apply/andP.
by split; apply: le_pmf_lim =>  ??; rewrite eq_fg.
Qed.

Lemma leub_pmf_limSource code fSource code muSource code : (forall nSource code, f n <=1 mu) -> pmf_lim f <=1 mu.
Proof.
by move => le x; rewrite -(pmf_limC mu x) le_pmf_lim. Qed.

Lemma pmf_lim_ubSource code fSource code kSource code : nondecreasing_fun (f : nat -> T -> R) -> f k <=1 pmf_lim f.
Proof.
move=> hf x; rewrite -lee_fin (EFin_pmf_lim_limn hf).
apply: lime_ge.
  apply: ereal_nondecreasing_is_cvgn => m n mn.
  by rewrite lee_fin; exact/lefP/hf.
near do (rewrite lee_fin; apply/lefP/hf).
exact: nbhs_infty_ge.
Unshelve. all: by end_near. Qed.

End Pmf_LimTheory.

Section Pmf_BindPmf_Lim.
Context {RSource code : realType} {TSource code USource code : choiceType}.

Implicit Types (f : (R.-pmf T)^nat) (h : T -> R.-pmf R).

Lemma pmf_limM_supSource code fSource code (hfSource code : nondecreasing_fun (f : _ -> _ -> _)) (cSource code : R) xSource code :
  0 <= c ->
  (pmf_lim f x * c)%:E = ereal_sup (range (fun nSource code => (f n x * c)%:E)).
Proof.
move=> c0.
rewrite EFinM (EFin_pmf_lim_sup hf) muleC.
rewrite (@ge0_ereal_supZl_range R T (fun tSource code nSource code => (f n t)%:E)) //=.
  by move=> t n; rewrite lee_fin.
congr ereal_sup; apply/seteqP; split.
- by move=> ? [x0 ?]; rewrite -EFinM mulrC=> <- //=; exists x0.
- by move=> ? [x0 ?]; rewrite mulrC EFinM => <- //=; exists x0.
Qed.

Lemma pmf_bind_limSource code fSource code hSource code : nondecreasing_fun (f : _ -> _ -> _) ->
  \bind_(xSource code <- pmf_lim f) h x =1 \lim_(nSource code) (\bind_(xSource code <- f n) h x).
Proof.
move=> ndf y; apply: EFin_inj.
have nd : nondecreasing_fun (fun nSource code => \bind_(iSource code <- f n) h i : _ -> _).
  move=> n m hnm; apply/lefP => x.
  by apply: le_pmf_bind => ?; exact/lefP/ndf.
rewrite EFin_pmf_bind (EFin_pmf_lim_sup nd).
under eq_esum => x _ do rewrite (pmf_limM_sup ndf)//.
rewrite (@exchange_esum_ereal_sup _ _ (fun xSource code nSource code => (f n x * h x y)%:E))/=.
- by move=> t n; rewrite lee_fin mulr_ge0.
- by move=> x n m nm; rewrite lee_fin ler_wpM2r//; exact/lefP/ndf.
congr ereal_sup; apply/seteqP; split.
- by move=> ? [x0 ?]; rewrite -EFin_pmf_bind => <- //=; exists x0.
- by move=> ? [x0 ?]; rewrite EFin_pmf_bind => <- //=; exists x0.
Qed.

End Pmf_BindPmf_Lim.

Section Pmf_LimPmf_Bind.
Context {RSource code : realType} {TSource code USource code : choiceType} (fSource code : nat -> T -> R.-pmf U).
Hypothesis hmonoSource code : forall xSource code nSource code mSource code, (n <= m)%N -> f n x <=1 f m x.

Lemma pmf_lim_bindSource code (muSource code : R.-pmf T) :
  \lim_(nSource code) \bind_(xSource code <- mu) (f n x) =1 \bind_(xSource code <- mu) \lim_(nSource code) (f n x).
Proof.
have nd1 y : nondecreasing_fun (f ^~ y : _ -> _ -> _).
  by move=> n m hnm; apply/lefP => x; exact: hmono.
have nd2 : nondecreasing_fun (fun nSource code => \bind_(iSource code <- mu) f n i : _ -> _).
  move=> n m hnm; apply/lefP => x.
  by apply: supp_le_pmf_bind => z _; exact: hmono.
move=> z; apply: EFin_inj.
rewrite (EFin_pmf_lim_sup nd2) EFin_pmf_bind.
under eq_esum => t _ do rewrite mulrC (pmf_limM_sup (nd1 t))//.
rewrite (@exchange_esum_ereal_sup _ _ (fun tSource code nSource code => (f n t z * mu t)%:E))/=.
- by move=> t n; rewrite lee_fin mulr_ge0.
- by move=> n m nm t; rewrite lee_fin ler_wpM2r// hmono.
congr ereal_sup; apply/seteqP; split.
- move=> ? [x0 ?]; rewrite EFin_pmf_bind => <-//=; exists x0 => //=.
  by under eq_esum do rewrite  mulrC.
- move=> ? [x0 ?]; under eq_esum do rewrite mulrC.
  by move => <- //=; exists x0 => //=; rewrite -EFin_pmf_bind.
Qed.

End Pmf_LimPmf_Bind.

Section Pmf_LimPmf_Lim.
Context {RSource code : realType} {TSource code : choiceType} (fSource code : nat -> nat -> R.-pmf T).
Hypotheses (hmono1Source code : forall kSource code n1Source code n2Source code, (n1 <= n2)%N -> f n1 k <=1 f n2 k)
           (hmono2Source code : forall kSource code n1Source code n2Source code, (n1 <= n2)%N -> f k n1 <=1 f k n2).

Local Lemma dhomo_apply2Source code : nondecreasing_fun (fun nSource code => f n n : _ -> R).
Proof.
move=> n m hnm; apply/lefP => x.
apply/le_trans.
  exact/hmono1/hnm.
exact: hmono2.
Qed.

Lemma leub_pmf_lim_pmf_lim1Source code : \lim_(nSource code) pmf_lim (f n) <=1 \lim_(nSource code) f n n.
Proof.
apply: leub_pmf_lim => n; apply: leub_pmf_lim => m x.
apply: (@le_trans _ _ (f (maxn n m) (maxn n m) x)).
  apply: (@le_trans _ _ (f (maxn n m) m x)).
  - by rewrite hmono1// leq_maxl.
  - by rewrite hmono2// leq_maxr.
by rewrite pmf_lim_ub//; exact : dhomo_apply2.
Qed.

Lemma pmf_lim_pmf_lim_ub1Source code : \lim_(nSource code) (f n n) <=1 \lim_(nSource code) \lim_(mSource code) f n m.
Proof.
apply: leub_pmf_lim => n x; apply: (@le_trans _ _ (pmf_lim (f n) x)).
- apply: pmf_lim_ub => ? ? ?.
  by apply/lefP => ?; exact: hmono2.
- apply: pmf_lim_ub => ? ? ?.
  by apply/lefP => ?; apply le_pmf_lim => ? ?; exact: hmono1.
Qed.

Lemma leub_pmf_lim_pmf_lim2Source code : \lim_(mSource code) (\lim_(nSource code) (f n m)) <=1 \lim_(nSource code) f n n.
Proof.
apply: leub_pmf_lim => n; apply: leub_pmf_lim => m x.
apply: (@le_trans _ _ (f (maxn n m) (maxn n m) x)).
  apply: (@le_trans _ _ (f (maxn n m) n x)).
  - by rewrite hmono1// leq_maxr.
  - by rewrite hmono2// leq_maxl.
by apply: pmf_lim_ub; exact : dhomo_apply2.
Qed.

Lemma pmf_lim_pmf_lim_ub2Source code : \lim_(nSource code) (f n n) <=1 \lim_(mSource code) (\lim_(nSource code) f n m).
Proof.
apply: leub_pmf_lim => n x; apply: (@le_trans _ _ (pmf_lim (fun kSource code => f k n) x)).
- apply: pmf_lim_ub => ? ? ?.
  by apply/lefP => ?; exact: hmono1.
- apply: pmf_lim_ub => ? ? ?.
  by apply/lefP => y; apply le_pmf_lim => ? ?; exact: hmono2.
Qed.

Lemma pmf_lim_pmf_limSource code :
  \lim_(nSource code) (\lim_(mSource code) (f n m)) =1 \lim_(mSource code) (\lim_(nSource code) f n m).
Proof.
move=> x; apply/eqP; rewrite eq_le; apply/andP; split.
- exact: (le_trans (leub_pmf_lim_pmf_lim1 x) (pmf_lim_pmf_lim_ub2 x)).
- exact: (le_trans (leub_pmf_lim_pmf_lim2 x) (pmf_lim_pmf_lim_ub1 x)).
Qed.

End Pmf_LimPmf_Lim.

Section pmf_bind_pmf_lim_diag.
Context {RSource code : realType} {TSource code USource code : choiceType}
  (dSource code : (R.-pmf T)^nat) (hSource code : nat -> T -> R.-pmf R).
Hypotheses (hdSource code : nondecreasing_fun (d : nat -> _ -> R))
           (HmonoSource code: forall kSource code n1Source code n2Source code, (n1 <= n2)%N -> h n1 k <=1 h n2 k).

Lemma nondecreasing_pmf_bindSource code (nmuSource code : nat -> R.-pmf T) :
  nondecreasing_fun (nmu : (_ -> R)^nat) ->
  nondecreasing_fun (fun nSource code => \bind_(iSource code <- nmu n) h n i : _ -> _).
Proof.
move=> hnmu n m hnm; apply/lefP => x; apply: supp_le_pmf_bind2.
  by move=> ?; exact/lefP/hnmu.
by move=> ? ? ?; exact: Hmono.
Qed.

Let pmf_bind_pmf_lim_diag_nSource code nSource code :
  \bind_(xSource code <- d n) (\lim_(nSource code) (h n x)) <=1 \lim_(nSource code) (\bind_(xSource code <- d n) h n x).
Proof.
move=> u; rewrite -(pmf_lim_bind Hmono); apply: leub_pmf_lim => m w.
apply: (@le_trans _ _ ((\bind_(iSource code <- d (maxn m n)) h (maxn m n) i) w)).
  apply: supp_le_pmf_bind2.
    by move=> x; exact/lefP/hd/leq_maxr.
  by move=> ? _; apply: Hmono; exact: leq_maxl.
exact: (@pmf_lim_ub _ _ (fun nSource code => \bind_(xSource code <- d n) h n x) (maxn m n)
  (nondecreasing_pmf_bind hd)).
Qed.

Lemma pmf_bind_pmf_lim_diagSource code :
  \bind_(xSource code <- \lim_(nSource code) d n) (\lim_(nSource code) (h n x)) =1
  \lim_(nSource code) (\bind_(xSource code <- d n) h n x).
Proof.
move=> z; apply/eqP; rewrite eq_le; apply/andP; split.
- rewrite pmf_bind_lim//; apply: leub_pmf_lim => n.
  exact: pmf_bind_pmf_lim_diag_n.
apply: leub_pmf_lim => n; apply: supp_le_pmf_bind2.
  exact: (@pmf_lim_ub _ _ d n hd).
move=> x _; apply: pmf_lim_ub => ? ? ?.
by apply/lefP => ?; exact: Hmono.
Qed.

End pmf_bind_pmf_lim_diag.

Section Marginals.
Context {RSource code : realType} {TSource code USource code : choiceType} (hSource code : T -> U) (muSource code : R.-pmf T).

Definition pmf_margin

pmf_margin : forall {R : realType} {T U : choiceType}, (T -> U) -> SubPmf.type R T -> SubPmf.type R U pmf_margin is not universe polymorphic Arguments pmf_margin {R T U} h%_function_scope mu pmf_margin is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_margin Declared in library mathcomp.analysis.probability_theory.subpmf, line 1194, characters 11-21


Source code
:= \bind_(xSource code <- mu) (pmf_ret (h x)).

End Marginals.

Section MarginalsTh.
Context {RSource code : realType} {TSource code USource code VSource code : choiceType}.

Lemma pmf_marginESource code (muSource code : R.-pmf T) (fSource code : T -> U) ySource code :
  (pmf_margin f mu) y = fine (\esum_(xSource code in [set: T]) ((f x == y)%:R * mu x)%:E).
Proof.
by congr fine; apply/eq_esum => x _; rewrite mulrC pmf_retE.
Qed.

Lemma pmf_bind_marginSource code (muSource code : R.-pmf T) (fSource code : T -> U) (gSource code : U -> R.-pmf V):
  \bind_(uSource code <- pmf_margin f mu) g u =1 \bind_(tSource code <- mu) (g (f t)).
Proof.
move=> x; rewrite pmf_bind_pmf_bind; apply: supp_eq_pmf_bind=> //.
by move=> y _ z /=; rewrite pmf_ret_bind.
Qed.

Lemma pmf_margin_bindSource code (muSource code : R.-pmf T) (fSource code : U -> V) (gSource code : T -> R.-pmf U):
  pmf_margin f (\bind_(tSource code <- mu) g t) =1 \bind_(tSource code <- mu) (pmf_margin f (g t)).
Proof.
exact/pmf_bind_pmf_bind. Qed.

Lemma pmf_margin_retSource code (tSource code : T) (fSource code : T -> U):
  pmf_margin f (pmf_ret t) =1 pmf_ret (f t) :> R.-pmf U.
Proof.
exact/pmf_ret_bind. Qed.

End MarginalsTh.

Lemma measure_of_pmf_marginSource code {RSource code : realType} {TSource code USource code : choiceType} (hSource code : T -> U)
    (muSource code : R.-pmf T) (SSource code : set (discrete_measurable_space U)) :
  measure_of_pmf (pmf_margin h mu) S
    = giry_map (measurable_fun_discreteT h) (measure_of_pmf mu) S.
Proof.

Notation pmf1Source code mu := (pmf_margin fst mu).
Notation pmf2Source code mu := (pmf_margin snd mu).

Section DSwap.
Context {RSource code : realType} {ASource code BSource code : choiceType} (muSource code : R.-pmf (A * B)%type).

Definition pmf_swap

pmf_swap : forall {R : realType} {A B : choiceType}, SubPmf.type R (A * B)%type -> SubPmf.type R (B * A)%type pmf_swap is not universe polymorphic Arguments pmf_swap {R A B} mu pmf_swap is transparent Expands to: Constant mathcomp.analysis.probability_theory.subpmf.pmf_swap Declared in library mathcomp.analysis.probability_theory.subpmf, line 1240, characters 11-19


Source code
: R.-pmf (B * A)%type :=
  pmf_margin (fun xySource code => (xy.2, xy.1)) mu.

End DSwap.

Section DSwapCoreTheory.
Context {RSource code : realType} {ASource code BSource code : choiceType} (muSource code : R.-pmf (A * B)%type).

Lemma pmf_swapESource code xySource code : pmf_swap mu xy = mu (xy.2, xy.1).
Proof.
rewrite /pmf_swap/= pmf_bindE (esumID [set (xy.2, xy.1)]).
  by move=> ? ?; rewrite lee_fin mulr_ge0.
rewrite setI1 //= in_setT esum_set1 esum1 //=.
  move=> ab [_ /eqP h] /=.
  rewrite pmf_ret0 ?mulr0 //.
  apply: contra h => /eqP <-/=.
  by rewrite -surjective_pairing.
by case: xy => x y /=; rewrite pmf_retxx mulr1 addr0.
Qed.

End DSwapCoreTheory.

Section DSwapTheory.
Context {RSource code : realType} {ASource code BSource code : choiceType} (muSource code : R.-pmf (A * B)%type).

Lemma pmf_swapKSource code : pmf_swap (pmf_swap mu) =1 mu.
Proof.
by case=> x y; rewrite !pmf_swapE. Qed.

Lemma supp_pmf_swapSource code xySource code : (xy.2, xy.1) \in supp mu ->
  xy \in supp (pmf_swap mu).
Proof.
by move=> h; apply/mem_suppP; rewrite pmf_swapE; exact/memNsuppP. Qed.

Lemma pmf1_swapSource code : pmf1 (pmf_swap mu) =1 pmf2 mu.
Proof.
move=> z; rewrite /pmf_margin [LHS]pmf_bind_pmf_bind => /=.
by apply/supp_eq_pmf_bind => // -[x y] _ t /=; rewrite pmf_ret_bind.
Qed.

Lemma pmf2_swapSource code : pmf2 (pmf_swap mu) =1 pmf1 mu.
Proof.
move=> z; rewrite /pmf_margin [LHS]pmf_bind_pmf_bind.
by apply/supp_eq_pmf_bind => // -[x y] _ t /=; rewrite pmf_ret_bind.
Qed.

End DSwapTheory.

Section DFst.
Context {RSource code : realType} {TSource code USource code : choiceType}.

Lemma pmf1ESource code (muSource code : R.-pmf (T * U)%type) xSource code :
  pmf1 mu x = fine (\esum_(ySource code in [set: U]) (mu (x, y))%:E).
Proof.
rewrite /pmf_margin /pmf_bind; congr fine.
rewrite (esumID [set zSource code : T * U | z.1 = x])/=.
  by move=> z _; rewrite lee_fin mulr_ge0.
rewrite [X in _ + X]esum1 ?adde0.
  by move=> z [_ /= /eqP zx]; rewrite pmf_ret0 ?mulr0.
rewrite (reindex_esum [set: U] _ (fun ySource code => (x, y))).
  split => //.
  - by move=> y1 y2 _ _ /(congr1 snd).
  - move=> z [_ /= zx]; exists z.2 => //=.
    by case: z zx => z1 z2 /= ->.
by apply: eq_esum => y _ /=; rewrite pmf_retxx mulr1.
Qed.

Lemma esummable_fstSource code (muSource code : R.-pmf (T * U)%type) xSource code :
  esummable [set: U] (fun ySource code => (mu (x, y))%:E).
Proof.
rewrite /esummable; apply: le_lt_trans (@subpmf_summable _ _ mu).
rewrite -(reindex_esum [set:U] [set zSource code : T * U | z.1 = x]
  (fun ySource code => (x, y)) (fun zSource code => (`|(mu z)%:E|)%E)).
  split => //.
  - by move=> y1 y2 _ _ /(congr1 snd).
  - move=> z /= zx; exists z.2 => //=.
    by case: z zx => z1 z2 /= ->.
by apply: subset_esum => //= z _; rewrite lee_tofin // mulr_ge0.
Qed.

End DFst.

Section DSnd.
Context {RSource code : realType} {TSource code USource code : choiceType}.

Lemma pmf2ESource code (muSource code : R.-pmf (T * U)%type) ySource code :
  pmf2 mu y = fine (esum [set:T] (fun xSource code => (mu (x, y))%:E)).
Proof.
by rewrite -pmf1_swap pmf1E; congr fine; apply/eq_esum=> x; rewrite pmf_swapE.
Qed.

Lemma esummable_sndSource code (muSource code : R.-pmf (T * U)%type) ySource code :
  esummable [set: T] (fun xSource code => (mu (x, y))%:E).
Proof.
have := esummable_fst (pmf_swap mu) y; apply/eq_esummable.
by move=> x /=; rewrite pmf_swapE.
Qed.

End DSnd.

Section pmf_esummable_lemmas.
Context {RSource code : realType} {TSource code : choiceType}.
Implicit Types (mu : R.-pmf T).

Lemma pmf_esummable_cstSource code muSource code cSource code : pmf_esummable mu (cst c).
Proof.
have : esummable setT (fun xSource code => c%:E * (mu x)%:E)%E.
  by apply: esummableZl => //; exact: subpmf_summable.
by apply/eq_esummable => x /= _; rewrite EFinM.
Qed.

Lemma pmf_esummableZSource code muSource code cSource code FSource code : pmf_esummable mu F -> pmf_esummable mu (c \*o F).
Proof.
move=> heF; rewrite /pmf_esummable.
have := esummableZl (f := EFin \o (fun xSource code => F x * mu x)) (c := c%:E) erefl heF.
apply/eq_esummable => x /=.
by rewrite -EFinM mulrA.
Qed.

Lemma bounded_pmf_esummableSource code muSource code FSource code :
  (exists MSource code, forall xSource code, `|F x| <= M) -> pmf_esummable mu F.
Proof.
case=> M leM; rewrite /pmf_esummable /esummable.
rewrite (eq_esum _ _ (fun xSource code => `|(F x)%:E * (mu x)%:E|)%E).
  by move=> x _; rewrite -EFinM.
apply: esummableMl; last exact: subpmf_summable.
by exists M%:E => [x ?|//]; rewrite /= lee_fin leM.
Qed.

Lemma esummable_pmfSource code muSource code FSource code : esummable [set: T] (EFin \o F) -> pmf_esummable mu F.
Proof.
move=> smF; rewrite /pmf_esummable /esummable.
rewrite (eq_esum _ _ (fun xSource code => `|(F x)%:E * (mu x)%:E|)%E).
  by move=> x _; rewrite -EFinM.
apply: esummableMr; last exact: smF.
by exists 1%:E => [x ?|//]; rewrite /= lee_fin ger0_norm// subpmf_le1.
Qed.

End pmf_esummable_lemmas.

Section pmf_expectation_lemmas.
Context {RSource code : realType} {TSource code : choiceType}.
Implicit Types (mu : R.-pmf T).

Lemma pmf_expectationZSource code muSource code fSource code cSource code : (forall xSource code, 0 <= f x) ->
  \E_[mu] (fun xSource code => c * (f x)) = c * (\E_[mu] f).
Proof.
move=> h1; rewrite /pmf_expectation.
under eq_esum => x do rewrite /= -mulrA EFinM.
have hnneg x : (0 <= (f x * mu x)%:E)%E by rewrite lee_fin mulr_ge0 ?h1.
by rewrite esumZ//= fineM.
Qed.

Lemma pmf_expectation_cstSource code muSource code rSource code : \E_[mu] (fun=> r) = \P_[mu] predT * r.
Proof.
have := pmf_expectationZ mu (f := fun=> 1%R) r.
by rewrite mulr1 => -> //=; rewrite mulrC.
Qed.

Lemma pmf_expectation0Source code muSource code : \E_[mu] (fun=> 0) = 0.
Proof.
by rewrite pmf_expectation_cst mulr0. Qed.

Lemma supp_eq_pmf_expectationSource code muSource code (f1Source code f2Source code : T -> R):
  {in supp mu, f1 =1 f2} -> \E_[mu] f1 = \E_[mu] f2.
Proof.
move=> eq_f; rewrite /pmf_expectation; congr fine; apply/eq_esum => x /=.
have [/eq_f -> //|] := boolP (x \in supp mu).
by move/memNsuppP => ->; rewrite !mulr0.
Qed.

Lemma pmf_expectationESource code muSource code (gSource code : T -> R) :
  \E_[mu] g = fine ('E_(measure_of_pmf mu) [EFin \o g]).
Proof.
by rewrite expectation_pmf. Qed.

Lemma pmf_expectationBSource code muSource code (fSource code gSource code : T -> R) :
    pmf_esummable mu f -> pmf_esummable mu g ->
  \E_[mu] (f \- g) = \E_[mu] f - \E_[mu] g.
Proof.

Lemma pmf_expectation_ge0Source code muSource code (gSource code : T -> R) :
  (forall ySource code, 0 <= g y) -> 0 <= \E_[mu] g.
Proof.
move=> g0; rewrite /pmf_expectation fine_ge0// esum_ge0// => x _.
by rewrite lee_fin mulr_ge0.
Qed.

End pmf_expectation_lemmas.

Section pmf_expectation_theory.
Context {RSource code : realType} {TSource code : choiceType}.
Implicit Types (mu : R.-pmf T) (A B E : pred T).

Local Open Scope ereal_scope.

Lemma expectation_pmf_retSource code (fSource code : T -> \bar R) (xSource code : T) :
  'E_(measure_of_pmf (pmf_ret x)) [f] = f x.
Proof.
rewrite expectation_def.
rewrite (eq_measure_integral (\d_(x : discrete_measurable_space T))).
  by move=> A _ _; exact: measure_of_pmf_ret.
by rewrite integral_dirac//= diracT mul1e.
Qed.

Lemma expectation_pmf_zeroSource code (fSource code : T -> \bar R) :
  'E_(measure_of_pmf pmf_zero) [f] = 0.
Proof.

Lemma expectation_pmf_bindSource code {USource code : choiceType} muSource code (nuSource code : T -> R.-pmf U) FSource code :
  (forall xSource code, 0 <= F x)%E ->
  'E_(measure_of_pmf (pmf_bind nu mu)) [F] =
  'E_(measure_of_pmf mu) [fun xSource code => 'E_(measure_of_pmf (nu x)) [F]].
Proof.
move=> HF; rewrite !expectation_def.
under [in RHS]eq_integral do rewrite expectation_def.
rewrite (eq_measure_integral
    (giry_bind (measure_of_pmf mu) (measurable_measure_of_pmf nu))).
  by move=> A _ _; exact: measure_of_pmf_bind_giry.
exact: giry_int_bind.
Qed.

Lemma expectation_pmf_marginSource code {USource code : choiceType} (hSource code : T -> U) muSource code (FSource code : U -> \bar R) :
  (forall ySource code, 0 <= F y)%E ->
  'E_(measure_of_pmf (pmf_margin h mu)) [F] = 'E_(measure_of_pmf mu) [F \o h].
Proof.
move=> F0; rewrite !expectation_def.
rewrite (eq_measure_integral
    (giry_map (measurable_fun_discreteT h) (measure_of_pmf mu))).
  by move=> A _ _; exact: measure_of_pmf_margin.
exact: giry_int_map.
Qed.

Local Open Scope ereal_scope.

Lemma expectation_pmf_bind_EFinSource code {USource code : choiceType} muSource code (nuSource code : T -> R.-pmf U)
    (gSource code : U -> R) :
  (forall ySource code, 0 <= g y)%R -> (forall etaSource code, pmf_esummable eta g) ->
  'E_(measure_of_pmf (pmf_bind nu mu)) [EFin \o g] =
  'E_(measure_of_pmf mu) [EFin \o (fun xSource code => \E_[nu x] g)].
Proof.
move=> g0 sg; rewrite (expectation_pmf_bind mu nu)//= !expectation_pmf.
apply: eq_esum => t _ //=; rewrite expectation_pmf; congr (_ * _)%E.
by rewrite /pmf_expectation fineK// (esummable_esum_fin_num (sg (nu _))).
Qed.

End pmf_expectation_theory.

Section pmf_expectation_dRV.
Import MeasurableR.
Context {dSource code} {TSource code : pmeasurableType d} {RSource code : realType} (PSource code : probability T R)
  (XSource code : {dRV P >-> R}).

Local Notation pmfXSource code := (@pmf d T R P X).

Local Open Scope ereal_scope.

Lemma measure_of_pmf_distributionSource code (ASource code : set R) :
  measurable A ->
  measure_of_pmf pmfX A = distribution P X A.
Proof.

Lemma ge0_expectation_pmfSource code (fSource code : R -> \bar R) :
  measurable_fun [set: R] f -> (forall rSource code, 0 <= f r) ->
 'E_(measure_of_pmf pmfX) [f] = \int[P]_wSource code f (X w).
Proof.
move=> mf f0; rewrite unlock /= -ge0_integral_distribution//.
have mid : measurable_fun [set: discrete_measurable_space R]
    (idfun : discrete_measurable_space R -> R) by [].
transitivity (\int[giry_map mid (measure_of_pmf pmfX)]_ySource code f y).
  exact/esym/giry_int_map.
by apply: eq_measure_integral => A mA _; exact: measure_of_pmf_distribution.
Qed.

Lemma expectation_pmf_integralSource code (fSource code : R -> \bar R) :
  measurable_fun [set: R] f ->
  'E_(measure_of_pmf pmfX) [f] = \int[P]_wSource code f (X w).
Proof.
move=> mf; rewrite unlock integralE [RHS]integralE; congr (_ - _).
- rewrite -[LHS]expectation_def ge0_expectation_pmf => //=.
    exact: measurable_funepos.
  by apply: eq_integral => w _; rewrite !funeposE.
- rewrite -[LHS]expectation_def ge0_expectation_pmf => //=.
     exact: measurable_funeneg.
  by apply: eq_integral => w _; rewrite !funenegE.
Qed.

Lemma expectation_pmf_expectationSource code :
  'E_(measure_of_pmf pmfX) [EFin] = 'E_P[EFin \o X].
Proof.
by rewrite expectation_pmf_integral //= unlock. Qed.

End pmf_expectation_dRV.

Section PrCoreTheory.
Context {RSource code : realType} {TSource code : choiceType}.
Implicit Types (mu : R.-pmf T) (A B E : pred T).

Lemma prESource code ESource code muSource code : (\P_[mu] E)%:E = \esum_(xSource code in [set: T]) ((E x)%:R * mu x)%:E.
Proof.
rewrite fineK//.
rewrite esum_abse; first by move=> ?; rewrite mulr_ge0.
rewrite -esummableE.
apply/(@le_esummable _ _ _ _ (EFin \o mu)) => [x _|//].
rewrite !lee_fin// mulr_ge0//=.
by rewrite ler_piMl //= lern1 leq_b1.
Qed.

Lemma prE_measureSource code muSource code ESource code : (\P_[mu] E)%:E = measure_of_pmf mu [set` E].
Proof.
rewrite prE /measure_of_pmf [RHS]esum_mkcond esum_setT_discrete.
apply: eq_esum => x _ /=; rewrite mem_setE unfold_in.
by case: (E x); rewrite ?mul1r ?mul0r.
Qed.

Lemma pmf_prob_pred0Source code muSource code : \P_[mu] pred0 = 0.
Proof.
by rewrite /pmf_prob esum1 // => x /=; rewrite mul0r. Qed.

Lemma pmf_prob_pred1Source code muSource code xSource code : \P_[mu] (pred1 x) = mu x.
Proof.
rewrite /pmf_prob.
rewrite (eq_esum _ _ (fun ySource code => if x == y then (mu y)%:E else 0)); last first.
 by rewrite esum_if_eq_op_set1.
move=> y _ /=; rewrite [y == x]eq_sym.
by case: ifP => xy; rewrite ?xy /= ?mul1r ?mul0r.
Qed.

Lemma pmf_prob_expectationSource code muSource code (QSource code : pred T) :
  \P_[mu] Q = \E_[mu] (fun mSource code => (Q m)%:R).
Proof.
by []. Qed.

Lemma pmf_prob_predTSource code muSource code : \P_[mu] predT = fine (esum [set: T] (EFin \o mu)).
Proof.
rewrite /pmf_prob /pmf_expectation; congr fine.
by apply/eq_esum=> x; rewrite mul1r.
Qed.

Lemma expectation_pmf_cstSource code muSource code rSource code :
  ('E_(measure_of_pmf mu) [cst r] = (\P_[mu] predT)%:E * r)%E.
Proof.
rewrite expectation_pmf esumZ; first by move=> ?; rewrite lee_fin.
rewrite muleC; congr (_ * _)%E.
rewrite pmf_prob_predT fineK//= esum_abse//=.
have := @subpmf_summable _ _ mu.
by rewrite esummableE.
Qed.

Lemma pmf_expectation_retSource code (fSource code : T -> R) (xSource code : T) : \E_[pmf_ret x] f = f x.
Proof.

Lemma pmf_prob_retSource code ESource code xSource code : \P_[pmf_ret x] E = (E x)%:R :> R.
Proof.

Lemma pmf_expectation_zeroSource code (fSource code : T -> R) : \E_[pmf_zero] f = 0.
Proof.

Lemma pmf_prob_zeroSource code ESource code : \P_[pmf_zero : R.-pmf T] E = 0.
Proof.

Lemma dweight_pmf_zeroSource code : dweight (@pmf_zero R T) = 0.
Proof.
exact: pmf_prob_zero. Qed.

Lemma pmf_expectation_marginSource code {USource code : choiceType} (hSource code : T -> U) muSource code (gSource code : U -> R) :
    (forall ySource code, 0 <= g y) ->
  \E_[pmf_margin h mu] g = \E_[mu] (g \o h).
Proof.
move=> g0.
have g0E y : (0 <= (EFin \o g) y)%E by rewrite /= lee_fin.
by rewrite /pmf_expectation -!expectation_pmfEFin (expectation_pmf_margin h mu).
Qed.

Lemma pmf_prob_ge0Source code ASource code muSource code : 0 <= \P_[mu] A.
Proof.
by rewrite -lee_fin prE_measure. Qed.

Lemma pmf_cprob_ge0Source code ASource code BSource code muSource code : 0 <= \P_[mu, B] A.
Proof.
by rewrite /pmf_cprob mulr_ge0 ?invr_ge0 // pmf_prob_ge0. Qed.

Lemma supp_eq_pmf_probSource code ASource code BSource code muSource code : {in supp mu, A =i B} -> \P_[mu] A = \P_[mu] B.
Proof.
move=> eq_AB; rewrite /pmf_prob; congr fine.
apply/eq_esum => x _ /=; case/boolP : (x \in supp mu).
- by move/eq_AB; rewrite -!topredE => /= ->.
- by move/memNsuppP => ->; rewrite !mulr0.
Qed.

Lemma eq_pmf_probSource code ASource code BSource code muSource code : A =i B -> \P_[mu] A = \P_[mu] B.
Proof.
by move=> eq_AB; apply/supp_eq_pmf_prob => x _; exact/eq_AB. Qed.

Lemma pred0_pmf_prob_eq0Source code (muSource code : R.-pmf T) (ESource code : pred T) :
  E =1 pred0 -> \P_[mu] E = 0.
Proof.
by move=> eq; rewrite -(pmf_prob_pred0 mu); exact/eq_pmf_prob. Qed.

End PrCoreTheory.

Section PrTheory.
Context {RSource code : realType} {TSource code USource code : choiceType} {ISource code : eqType}.
Implicit Types (mu : R.-pmf T) (A B E : pred T).

Lemma pmf_prob_eq0Source code ASource code muSource code :
  (forall xSource code, x \in supp mu -> x \notin A) -> \P_[mu] A = 0.
Proof.
move=> h; rewrite /pmf_prob esum1 //= => x ?; congr EFin.
apply/eqP; rewrite mulf_eq0 orbC; have [//|/=] := eqVneq (mu x) 0.
by move/h; rewrite -topredE /= => /negbTE ->.
Qed.

Lemma pmf_cprob_eq0Source code ASource code BSource code muSource code :
  (forall xSource code, x \in supp mu -> x \in B -> x \notin A) ->
  \P_[mu, B] A = 0.
Proof.
move=> h; rewrite /pmf_cprob pmf_prob_eq0 ?mul0r // => x /h {h} /orb_idl.
by rewrite negb_and /= => <-; rewrite orbAC orbN.
Qed.

Lemma subset_pmf_probSource code ASource code BSource code muSource code : {subset B <= A} -> \P_[mu] B <= \P_[mu] A.
Proof.
move=> le_BA; rewrite -lee_fin !prE.
apply: le_esum => x _; rewrite lee_fin.
rewrite ler_wpM2r // ?ge0_mu.
rewrite ler_nat; have := le_BA x; rewrite -!topredE /=.
by case: (B x) => // ->.
Qed.

Lemma pmf_prob_le1Source code ASource code muSource code : \P_[mu] A <= 1.
Proof.
apply: (@le_trans _ _ \P_[mu] predT); first by apply/subset_pmf_prob.
rewrite pmf_prob_predT -fine1 fine_le//; [|exact: subpmf_sum_le1].
rewrite ge0_fin_numE.
  by apply: esum_ge0 => x _; rewrite lee_fin.
by apply: le_lt_trans; [exact: subpmf_sum_le1|rewrite ltey].
Qed.

Lemma pmf_prob_bindSource code ESource code fSource code (muSource code : R.-pmf U) :
  \P_[pmf_bind f mu] E = \E_[mu] (fun xSource code => \P_[f x] E).
Proof.
have fin : esummable [set: U] (fun xSource code => ((\P_[f x] E) * mu x)%:E).
  apply/(le_esummable (g := EFin \o mu)) => [x _|//].
  by rewrite !lee_fin mulr_ge0 ?pmf_prob_ge0//= ler_piMl ?pmf_prob_le1.
apply: EFin_inj; rewrite prE_measure measure_of_pmf_bind.
under eq_integral do rewrite -prE_measure.
rewrite -expectation_def expectation_pmfEFin /pmf_expectation fineK//.
exact: esummable_esum_fin_num.
Qed.

Lemma pmf_prob_marginSource code ESource code fSource code (muSource code : R.-pmf U) :
  \P_[pmf_margin f mu] E = \P_[mu] [pred xSource code | f x \in E].
Proof.

Lemma pmf_expectation_leSource code muSource code f1Source code f2Source code :
    pmf_esummable mu f1 -> pmf_esummable mu f2 -> f1 <=1 f2 ->
  \E_[mu] f1 <= \E_[mu] f2.
Proof.
move=> sm1 sm2 le_f; rewrite /pmf_expectation; apply: fine_le.
- exact: (esummable_esum_fin_num sm1).
- exact: (esummable_esum_fin_num sm2).
- by apply: le_esum => x /=; rewrite lee_fin ler_wpM2r ?ge0_mu.
Qed.

Lemma le_mem_pmf_probSource code E1Source code E2Source code muSource code :
    (forall xSource code, x \in supp mu -> x \in E1 -> x \in E2) ->
  \P_[mu] E1 <= \P_[mu] E2.
Proof.
move=> le; rewrite -lee_fin !prE.
apply: le_esum => x _; rewrite lee_fin.
case/boolP: (x \in supp mu) => [xin|/memNsuppP ->]; last by rewrite !mulr0.
have hE : (E1 x)%:R <= (E2 x)%:R :> R.
  rewrite ler_nat; have := le x xin; rewrite -!topredE /=.
  by case: (E1 x) => // ->.
by rewrite ler_wpM2r // ?ge0_mu.
Qed.

Lemma le_pmf_pmf_probSource code ASource code (muSource code nuSource code : R.-pmf T) :
    (forall xSource code, x \in supp nu -> x \in A -> nu x <= mu x) ->
  \P_[nu] A <= \P_[mu] A.
Proof.
move=> h; rewrite -lee_fin !prE.
apply: le_esum => x _; rewrite lee_fin.
case/boolP : (x \in supp nu) => [/h {}h|]; last first.
  by move/memNsuppP => ->; rewrite mulr0 mulr_ge0// ler0n.
by case/boolP: (A x) => [/h|]; rewrite ?(mul0r, mul1r).
Qed.

Lemma pmf_cprob_le1Source code ASource code BSource code muSource code : \P_[mu, B] A <= 1.
Proof.
move: (pmf_prob_ge0 B mu); rewrite /pmf_cprob le_eqVlt.
case/predU1P => [<-|]; first by rewrite invr0 mulr0 ler01.
move/ler_pdivrMr=> ->; rewrite mul1r.
by apply: le_mem_pmf_prob => x _ /andP[].
Qed.

Lemma pmf_cprob_sumSource code ASource code muSource code : 0 < \P_[mu] A ->
  fine (\esum_(xSource code in [set: T]) (\P_[mu, A] (pred1 x))%:E) = 1.
Proof.
move=> gt0.
have a0 i : 0 <= (A i)%:R * mu i by rewrite mulr_ge0// ler0n.
have hpr1 x : \P_[mu] [predI pred1 x & A] = (A x)%:R * mu x.
  rewrite /pmf_prob.
  rewrite (eq_esum _ _ ( fun ySource code => if x == y then ((A y)%:R * mu y)%:E else 0)).
    move=> y _ /=; rewrite !inE [y == x]eq_sym.
    by case: ifP => xy; rewrite ?mul0r.
  by rewrite esum_if_eq_op_set1.
rewrite (eq_esum _ _ (fun xSource code => ((\P_[mu] A)^-1 * ((A x)%:R * mu x))%:E)).
  by move=> x _; rewrite /pmf_cprob hpr1; congr (_ %:E); rewrite mulrC.
rewrite (EFin_esumZ _ _ a0).
  by rewrite invr_ge0// pmf_prob_ge0.
by rewrite -prE -EFinM mulVf// gt_eqF.
Qed.

Lemma eq0_pmf_probSource code muSource code ESource code : \P_[mu] E = 0 -> forall xSource code, x \in E -> mu x = 0.
Proof.
move=> hE x xE.
have hle : \P_[mu] (pred1 x) <= \P_[mu] E.
  by apply: subset_pmf_prob => y; rewrite !inE => /eqP ->.
move: hle; rewrite hE pmf_prob_pred1 => h.
by apply/eqP; rewrite eq_le h /=.
Qed.

Lemma pmf_probIDSource code ASource code BSource code muSource code :
  \P_[mu] A = \P_[mu] [predI A & B] + \P_[mu] [predI A & predC B].
Proof.
have ge0 (PSource code : pred T) iSource code : (0 <= ((P i)%:R * mu i)%:E)%E.
  by rewrite lee_fin mulr_ge0 ?ler0n ?ge0_mu.
apply: EFin_inj; rewrite EFinD !prE.
rewrite -(esumD (fun iSource code _ => ge0 _ i) (fun iSource code _ => ge0 _ i)).
apply: eq_esum => x _; rewrite -EFinD; congr (_ %:E).
rewrite !inE -!topredE /= -mulrDl; congr (_ * _).
rewrite -natrD; congr (_ %:R).
by case: (A x); case: (B x).
Qed.

Lemma pmf_prob_predU_indepSource code (ASource code BSource code : pred T) (muSource code : R.-pmf T) :
    (forall xSource code, x \in A -> x \notin B) ->
  \P_[mu] [predU A & B] = \P_[mu] A + \P_[mu] B.
Proof.
move=> dsj.
have ge0 (PSource code : pred T) iSource code : (0 <= ((P i)%:R * mu i)%:E)%E.
  by rewrite lee_fin mulr_ge0 ?ler0n ?ge0_mu.
apply: EFin_inj; rewrite EFinD !prE.
rewrite -(esumD (fun iSource code _ => ge0 _ i) (fun iSource code _ => ge0 _ i)).
apply: eq_esum => x _; rewrite -EFinD; congr (_ %:E).
rewrite !inE -!topredE /= -mulrDl; congr (_ * _).
rewrite -natrD; congr (_ %:R).
case/boolP: (A x) => Ax; case/boolP: (B x) => Bx //=.
by move/dsj: Ax; rewrite -topredE /= Bx.
Qed.

Lemma pmf_prob_mem_mapSource code fSource code muSource code (rSource code : seq U) : uniq r ->
  \P_[mu] [pred xSource code | f x \in r] =
  \sum_(ySource code <- r) \P_[mu] [pred xSource code | f x == y].
Proof.
elim: r => [_|y r ih]; first by rewrite big_nil pred0_pmf_prob_eq0.
case/andP => yNr /ih {ih}h; rewrite big_cons -h -pmf_prob_predU_indep.
  by move=> x; rewrite !inE => /eqP ->.
exact/eq_pmf_prob.
Qed.

Lemma pmf_prob_memSource code muSource code (rSource code : seq T) : uniq r ->
  \P_[mu] [pred xSource code | x \in r] = \sum_(xSource code <- r) mu x.
Proof.
elim: r => [_|y r ih]; first by rewrite big_nil pred0_pmf_prob_eq0//.
case/andP=> yNr /ih {ih}h; rewrite big_cons /= -pmf_prob_pred1.
by rewrite -h -pmf_prob_predU_indep // => x /eqP ->.
Qed.

Lemma pmf_prob_bigor_indepSource code muSource code (PSource code : I -> pred T) (rSource code : seq I) : uniq r ->
  (forall p1Source code p2Source code xSource code,
    p1 != p2 -> p1 \in r -> p2 \in r -> x \in P p1 -> x \notin P p2) ->
  \P_[mu] [pred xSource code | has [pred pSource code | x \in P p] r] =
  \sum_(pSource code <- r) \P_[mu] (P p).
Proof.
move=> uq_r dj; pose S xSource code := \big[orb/false]_(pSource code <- r) (x \in P p).
rewrite (@eq_pmf_prob _ _ _ S) => [x|]; first by rewrite !inE -big_has.
rewrite {}/S; elim: r uq_r dj => [_|p r ih /andP[pNr /ih {ih}h]] dj.
 by rewrite big_nil pred0_pmf_prob_eq0 // => x; rewrite big_nil.
rewrite big_cons -h => [p1 p2 x ne_p p1r p2r|].
  by apply/dj=> //; rewrite in_cons (p1r, p2r) orbT.
rewrite -pmf_prob_predU_indep => [x xNPp|].
  rewrite -topredE /= big_has; apply/hasPn => y y_in_r.
  apply/(dj p); rewrite ?in_cons ?(eqxx, y_in_r, orbT) //.
  by apply: contra pNr => /eqP ->.
by apply/eq_pmf_prob => x; rewrite -!topredE /= big_cons.
Qed.

Lemma pmf_prob_predUSource code ASource code BSource code muSource code : \P_[mu] [predU A & B] =
  \P_[mu] A + \P_[mu] B - \P_[mu] [predI A & B].
Proof.
apply/eqP; rewrite eq_sym subr_eq [in X in _==X]addrC; apply/eqP.
rewrite (pmf_probID _ B) -addrA -pmf_prob_predU_indep => [x|].
  by rewrite !inE => /andP[].
congr (_ + _); apply/eq_pmf_prob => x; rewrite !inE -!topredE /=.
by apply/orb_id2r => /negbTE ->; rewrite andbT.
Qed.

Lemma pmf_prob_predISource code ASource code BSource code muSource code : \P_[mu] [predI A & B] =
  \P_[mu] A + \P_[mu] B - \P_[mu] [predU A & B].
Proof.
by rewrite pmf_prob_predU subKr. Qed.

Lemma le_pmf_prob_predUSource code ASource code BSource code muSource code : \P_[mu] [predU A & B] <= \P_[mu] A + \P_[mu] B.
Proof.

Lemma le_pmf_prob_predISource code ASource code BSource code muSource code : \P_[mu] [predI A & B] <= \P_[mu] A + \P_[mu] B.
Proof.

Lemma pmf_prob_predCSource code ESource code muSource code: \P_[mu](predC E) = \P_[mu] predT - \P_[mu] E.
Proof.
apply/esym/eqP; rewrite subr_eq -pmf_prob_predU_indep//.
by apply/eqP/eq_pmf_prob => x; rewrite !inE orNb.
Qed.

Lemma pmf_prob_splitSource code BSource code ASource code muSource code : \P_[mu] A =
    \P_[mu]        B  * \P_[mu,       B] A
  + \P_[mu] (predC B) * \P_[mu, predC B] A.
Proof.
suff h A' B': \P_[mu] [predI A' & B'] = \P_[mu] B' * \P_[mu, B'] A'.
  by rewrite (pmf_probID _ B); congr (_ + _); exact/h.
rewrite /pmf_cprob mulrCA; have [] := eqVneq (\P_[mu] B') 0; last first.
  by move=> nzPB'; rewrite divff // mulr1.
move=> zPB'; rewrite zPB' invr0 !mulr0; apply/pmf_prob_eq0.
move=> x mux; move/eq0_pmf_prob: zPB' => /(_ x) h; rewrite !inE.
by apply/negP=> /andP[_ /h] /mem_suppP.
Qed.

Lemma pmf_cprob_pred1Source code (muSource code : R.-pmf T) xSource code ASource code :
  \P_[mu, A] (pred1 x) = (A x)%:R * mu x / \P_[mu] A.
Proof.
rewrite /pmf_cprob; congr (_ / _); rewrite /pmf_prob.
rewrite (eq_esum _ _ (fun ySource code => if x == y then ((A y)%:R * mu y)%:E else 0)).
  move=> y _ /=; rewrite !inE [y == x]eq_sym.
  by case: ifP => xy; rewrite ?xy /= ?mul1r ?mul0r.
by rewrite esum_if_eq_op_set1.
Qed.

Lemma esummable_pmf_prob_fSource code fSource code muSource code (PSource code : pred T):
  pmf_esummable mu f ->
  esummable [set: T] (EFin \o (fun xSource code => (P x)%:R * (f x * mu x))).
Proof.
move=> sm; rewrite /esummable //= .
rewrite (eq_esum _ _ (fun xSource code => `|((P x)%:R)%:E * (f x * mu x)%:E|)%E).
  by move => i _ //; rewrite -abseM.
apply: esummableMl; last exact: sm.
exists 1%:E => [x|//].
by rewrite gee0_abs ?lee_fin ?ler0n// lern1 leq_b1.
Qed.

Lemma esum_is_fin_num_mu_fSource code fSource code muSource code (PSource code : pred T):
  pmf_esummable mu f ->
  \esum_(xSource code in [set: T]) ((P x)%:R * (f x * mu x))%:E \is a fin_num.
Proof.
by move=> ?; apply: esummable_esum_fin_num; exact: esummable_pmf_prob_f.
Qed.

Lemma pmf_prob_cexpectation_esumSource code muSource code fSource code (PSource code : pred T):
    pmf_esummable mu f ->
  \P_[mu] P * \E_[mu, P] f =
  fine (\esum_(xSource code in [set: T]) ((P x)%:R * (f x * mu x))%:E).
Proof.
move=> sm.
have [c0|cn0] := eqVneq (\P_[mu] P) 0.
  rewrite c0 mul0r [X in fine X](_ : _ = 0) ?fine0//.
  rewrite esum1// => x _.
  apply/eqP; rewrite eqe mulrCA mulf_eq0; apply/orP; right.
  have [Px|]/= := boolP (P x); last by rewrite mul0r.
  by rewrite mul1r (eq0_pmf_prob _ Px).
rewrite /pmf_cexpectation.
rewrite (eq_esum _ _ (fun xSource code => (\P_[mu] P)^-1 %:E * ((P x)%:R * (f x * mu x))%:E)%E).
  move=> x ?.
  rewrite /= pmf_cprob_pred1 -EFinM; congr (_ %:E).
  by rewrite mulrA mulrC; congr (_ * _); exact: mulrCA.
rewrite (esummable_esumZ _ (esummable_pmf_prob_f P sm)).
  by rewrite abse_fin_num.
by rewrite fineM ?abse_fin_num ?sum_fin_num_mu_f// mulVKf.
Qed.

Lemma pmf_expectation_splitSource code ASource code fSource code muSource code : pmf_esummable mu f ->
  \E_[mu] f = \P_[mu]        A  * \E_[mu,       A] f
            + \P_[mu] (predC A) * \E_[mu, predC A] f.
Proof.
move=> sm.
rewrite [in RHS](pmf_prob_cexpectation_esum A)//.
rewrite [in RHS](pmf_prob_cexpectation_esum (predC A))//.
rewrite [in LHS]/pmf_expectation -fineD ?esum_is_fin_num_mu_f//.
congr fine.
rewrite -esummable_esumD//; [exact: esummable_pmf_prob_f..|].
apply: eq_esum => x ? /=; rewrite -EFinD; congr EFin.
by rewrite -mulrDl -natrD addnC addn_negb mul1r.
Qed.

Lemma bounded_pmf_expectation_leSource code muSource code FSource code (MSource code : R) :
  0 <= M -> (forall xSource code, `|F x| <= M) -> \E_[mu] F <= M.
Proof.
move=> ge0M bd; apply/(@le_trans _ _ (\E_[mu] (fun _ => M))).
  apply/pmf_expectation_le.
  - by apply/bounded_pmf_esummable; exists M.
  - exact/pmf_esummable_cst.
  - by move=> x; apply/(le_trans _ (bd x))/ler_norm.
by rewrite pmf_expectation_cst ler_piMl // pmf_prob_le1.
Qed.

Lemma ge0_pmf_expectation_bindSource code (muSource code : R.-pmf T) (nuSource code : T -> R.-pmf U) (gSource code : U -> R) :
    (forall ySource code, 0 <= g y) -> (forall etaSource code, pmf_esummable eta g) ->
  \E_[pmf_bind nu mu] g = \E_[mu] (fun xSource code => \E_[nu x] g).
Proof.
move=> g0 sg; rewrite [LHS]pmf_expectationE expectation_pmf_bind_EFin//.
by rewrite -pmf_expectationE.
Qed.

Lemma le_pmf_esummableSource code {VSource code : choiceType} (muSource code : R.-pmf V) (kSource code hSource code : V -> R) :
  (forall xSource code, `|k x| <= `|h x|) -> pmf_esummable mu h -> pmf_esummable mu k.
Proof.
move=> kh; rewrite /pmf_esummable => fh.
rewrite esummableE ge0_fin_numE.
  by apply: esum_ge0 => i _; exact: abse_ge0.
apply: (le_lt_trans _ fh); apply: le_esum.
by move => ? ?; rewrite lee_fin !normrM ler_wpM2r.
Qed.

Lemma pmf_esummable_expectationSource code (muSource code : R.-pmf T) (nuSource code : T -> R.-pmf U) (gSource code : U -> R) :
    (forall ySource code, 0 <= g y) -> (forall etaSource code, pmf_esummable eta g) ->
  pmf_esummable mu (fun xSource code => \E_[nu x] g).
Proof.
move=> g0 sg.
have hge0 x : 0 <= \E_[nu x] g * mu x by rewrite mulr_ge0// pmf_expectation_ge0.
rewrite /pmf_esummable esummableE.
rewrite (eq_esum _ _ (EFin \o (fun xSource code => \E_[nu x] g * mu x))).
  by move=> x _ /=; rewrite ger0_norm.
rewrite -expectation_pmfEFin -(expectation_pmf_bind_EFin mu nu g0 sg).
rewrite expectation_pmfEFin.
exact: (esummable_esum_fin_num (sg (pmf_bind nu mu))).
Qed.

Lemma pmf_expectation_bindSource code muSource code (nuSource code : T -> R.-pmf U ) FSource code :
    (forall etaSource code, pmf_esummable eta F) ->
  \E_[pmf_bind nu mu] F = \E_[mu] (fun xSource code => \E_[nu x] F).
Proof.
move=> sF.
have spF eta : pmf_esummable eta F^\+.
  exact: (le_pmf_esummable (le_funrpos F) (sF eta)).
have snF eta : pmf_esummable eta F^\-.
  exact: (le_pmf_esummable (le_funrneg F) (sF eta)).
transitivity (\E_[mu] (fun xSource code => \E_[nu x] F^\+)
            - \E_[mu] (fun xSource code => \E_[nu x] F^\-)).
  rewrite -(ge0_pmf_expectation_bind mu nu _ spF)//.
  rewrite -(ge0_pmf_expectation_bind mu nu _ snF) //.
  rewrite -(pmf_expectationB (spF _) (snF _)); congr (\E_[pmf_bind nu mu] _).
  exact/esym/funrposBneg.
rewrite -(pmf_expectationB (pmf_esummable_expectation mu nu _ spF)
  (pmf_esummable_expectation mu nu _ snF))//.
apply: supp_eq_pmf_expectation => x _ /=.
rewrite -(pmf_expectationB (spF _) (snF _)).
by congr (\E_[nu x] _); exact: funrposBneg.
Qed.

End PrTheory.

Section mono_esum.
Context {RSource code : realType} {TSource code : choiceType} {fSource code : (R.-pmf T)^nat}.
Hypothesis ndfSource code : nondecreasing_fun (f : _ -> _ -> _).

Lemma nondecreasing_esum_EfnSource code (ESource code : T -> \bar R):
  (forall mSource code, 0 <= E m)%E ->
  forall mSource code nSource code, (m <= n)%N ->
  (\esum_(xSource code in [set:T]) (E x * (f m x)%:E) <=
   \esum_(xSource code in [set:T]) (E x * (f n x)%:E))%E.
Proof.
move=> hE m n le_mn.
by apply/le_esum => x _; rewrite lee_pmul ?lee_fin//=; exact/lefP/ndf.
Qed.

Lemma esum_pmf_lim_rSource code [ESource code : T -> \bar R] [rSource code : \bar R]:
  (forall mSource code, 0 <= E m)%E ->
  (forall nSource code, \esum_(xSource code in [set: T]) (E x * (f n x)%:E) <= r)%E ->
  (\esum_(xSource code in [set: T]) (E x * ((pmf_lim f) x)%:E) <= r)%E.
Proof.
move => hE h.
rewrite (@eq_esum _ _ _ _ (fun xSource code => ereal_sup (range (fun nSource code =>  E x * (f n x)%:E)%E))).
   move => ? ?; rewrite EFin_pmf_lim_sup//.
  rewrite (@ge0_ereal_supZl_range R T (fun aSource code bSource code => (f b a)%:E)) //.
  by move => ? ?; rewrite lee_fin.
rewrite exchange_esum_ereal_sup//.
- by move => ? ?; rewrite mule_ge0 ?lee_fin.
- by move => ? ? ? ?; rewrite lee_pmul ?lee_fin //; exact/lefP/ndf.
by rewrite ge_ereal_sup//= => x [n s] <-; exact: h.
Qed.

Lemma esum_pmf_lim_r_rSource code [ESource code : T -> R] [rSource code : \bar R]:
  (forall mSource code, 0 <= E m) ->
  (forall nSource code, \esum_(xSource code in [set: T]) ((E x * f n x)%:E) <= r)%E ->
  (\esum_(xSource code in [set: T]) (E x * (pmf_lim f) x)%:E <= r)%E.
Proof.
move=> hE h.
rewrite (eq_esum _ _ (fun xSource code : T => ((E x)%:E * ((pmf_lim f) x)%:E)%E)).
  by move => ? _; rewrite EFinM.
apply: (esum_pmf_lim_r (E := fun xSource code => (E x)%:E)).
  by move=> m; rewrite lee_fin.
move=> n; under eq_esum => x _ do rewrite -EFinM.
exact: h n.
Qed.

Lemma esum_pmf_limSource code (ESource code : pred T) :
  \esum_(xSource code in [set: T]) ((E x)%:R * (pmf_lim f) x)%:E =
  limn (fun nSource code => \esum_(xSource code in [set: T]) ((E x)%:R * f n x)%:E).
Proof.
have hmono n m : (n <= m)%N -> forall xSource code,
    (((E x)%:R * f n x)%:E <= ((E x)%:R * f m x)%:E)%E.
  move=> nm x; rewrite lee_fin ler_wpM2l ?ler0n//.
  exact/lefP/ndf.
transitivity (ereal_sup (range
    (fun nSource code => \esum_(xSource code in [set: T]) ((E x)%:R * f n x)%:E))); last first.
  apply: sup_range_lim => n m nm; apply: le_esum => x _.
  exact: (hmono n m nm x).
rewrite -(@exchange_esum_ereal_sup _ _ (fun xSource code nSource code => ((E x)%:R * f n x)%:E))//.
- by move=> x n; rewrite lee_fin mulr_ge0// ler0n.
- by move => x n m nm; exact: hmono.
apply: eq_esum => x _.
rewrite EFinM EFin_pmf_lim_sup//.
rewrite (@ge0_ereal_supZl_range R T (fun aSource code bSource code => (f b a)%:E)) //.
  by move=> t n; rewrite lee_fin.
by rewrite lee_fin.
Qed.

End mono_esum.