-
Notifications
You must be signed in to change notification settings - Fork 2
/
dist.v
268 lines (221 loc) · 8.18 KB
/
dist.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
Set Implicit Arguments.
Unset Strict Implicit.
Require Import mathcomp.ssreflect.ssreflect.
From mathcomp Require Import all_ssreflect.
From mathcomp Require Import all_algebra.
Import GRing.Theory Num.Def Num.Theory.
Local Open Scope ring_scope.
(** Discrete distributions *)
Section Dist.
Variable T : finType.
Variable rty : numDomainType.
Definition dist_axiom (f : {ffun T -> rty}) :=
[&& \sum_t (f t) == 1
& [forall t : T, f t >= 0]].
Record dist : Type := mkDist { pmf :> {ffun T -> rty}; dist_ax : dist_axiom pmf }.
Canonical dist_subType := [subType for pmf].
(* We have eqType and choiceTypes on distributions:*)
Definition dist_eqMixin := Eval hnf in [eqMixin of dist by <:].
Canonical dist_eqType := Eval hnf in EqType dist dist_eqMixin.
Definition dist_choiceMixin := [choiceMixin of dist by <:].
Canonical dist_choiceType := Eval hnf in ChoiceType dist dist_choiceMixin.
End Dist.
Section distLemmas.
Variable T : finType.
Variable rty : numDomainType.
Variable d : dist T rty.
Lemma dist_normalized : \sum_t d t = 1.
Proof. by case: (andP (dist_ax d)); move/eqP. Qed.
Lemma dist_positive : forall t : T, d t >= 0.
Proof. by case: (andP (dist_ax d))=> _; move/forallP. Qed.
End distLemmas.
Section support.
Variable T : finType.
Variable rty : numDomainType.
Variable d : dist T rty.
Definition support : {set T} := [set t : T | 0 < d t].
Lemma in_support x : x \in support -> 0 < d x.
Proof. by rewrite /support in_set. Qed.
Lemma supportP x : x \in support <-> 0 < d x.
Proof.
split; first by apply: in_support.
by rewrite /support in_set.
Qed.
End support.
Section bind.
Variable T U : finType.
Variable rty : numDomainType.
Variable d : {ffun T -> rty}.
Variable f : T -> {ffun U -> rty}.
Definition bind : {ffun U -> rty} :=
finfun (fun u : U => \sum_(t : T) (d t) * (f t u)).
End bind.
Section expectedValue.
Variable T : finType.
Variable rty : numDomainType.
Variable d : dist T rty.
Definition probOf (p : pred T) :=
\sum_(t : T | p t) d t.
Lemma probOf_xpredT : probOf xpredT = 1.
Proof.
rewrite /probOf; apply: dist_normalized.
Qed.
Definition expectedCondValue (f : T -> rty) (p : pred T) :=
(\sum_(t : T | p t) (d t) * (f t)) / (probOf p).
Lemma expectedCondValue_mull f p c :
expectedCondValue (fun t => c * f t) p = c * expectedCondValue f p.
Proof.
rewrite /expectedCondValue.
have ->: \sum_(t | p t) d t * (c * f t)
= c * \sum_(t | p t) d t * f t.
{ rewrite mulr_sumr; apply/congr_big=> //= i _.
by rewrite mulrA [d i * c]mulrC -mulrA. }
by rewrite mulrA.
Qed.
Lemma expectedCondValue_linear f g p :
expectedCondValue (fun t => f t + g t) p =
expectedCondValue f p + expectedCondValue g p.
Proof.
rewrite /expectedCondValue.
have ->: \sum_(t | p t) d t * (f t + g t) =
\sum_(t | p t) (d t * f t + d t * g t).
{ by apply/congr_big=> //= i _; rewrite mulrDr. }
rewrite 3!mulr_suml -big_split /=; move: (probOf p) => e.
apply: congr_big => // i _; rewrite mulrDl //.
Qed.
Definition expectedValue (f : T -> rty) :=
\sum_(t : T) (d t) * (f t).
Lemma expectedValue_expectedCondValue f :
expectedValue f = expectedCondValue f xpredT.
Proof.
by rewrite /expectedValue /expectedCondValue probOf_xpredT divr1.
Qed.
Lemma expectedValue_mull f c :
expectedValue (fun t => c * f t) = c * expectedValue f.
Proof. by rewrite 2!expectedValue_expectedCondValue expectedCondValue_mull. Qed.
Lemma expectedValue_linear f g :
expectedValue (fun t => f t + g t) =
expectedValue f + expectedValue g.
Proof. by rewrite 3!expectedValue_expectedCondValue expectedCondValue_linear. Qed.
Lemma expectedValue_const c : expectedValue (fun _ => c) = c.
Proof.
rewrite /expectedValue /expectedCondValue -mulr_suml.
by case: (andP (dist_ax d)); move/eqP=> ->; rewrite mul1r.
Qed.
Lemma expectedValue_range f
(H : forall x : T, 0 <= f x <= 1) :
0 <= expectedValue f <= 1.
Proof.
rewrite /expectedValue /expectedCondValue; apply/andP; split.
apply: sumr_ge0=> i _; case H2: (f i == 0).
{ by move: (eqP H2)=> ->; rewrite mulr0. }
{ rewrite mulrC pmulr_rge0; first by apply: dist_positive.
case: (andP (H i))=> H3 _.
rewrite lt0r; apply/andP; split=> //.
by apply/eqP=> H4; rewrite H4 eq_refl in H2. }
rewrite -(@dist_normalized T rty d); apply: ler_sum.
move=> i _; case H2: (d i == 0).
{ by move: (eqP H2)=> ->; rewrite mul0r. }
rewrite mulrC ger_pmull; first by case: (andP (H i)).
have H3: 0 <= d i by apply: dist_positive.
rewrite ltr_def; apply/andP; split=> //.
by apply/eqP=> H4; rewrite H4 eq_refl in H2.
Qed.
End expectedValue.
Section cdf.
Variable T : finType.
Variable rty : numDomainType.
Variable d : dist T rty.
Fixpoint cdf_aux (x : T) (l : seq T) : rty :=
if l is [:: y & l'] then
if x == y then d y
else d x + cdf_aux x l'
else 0.
Definition cdf (x : T) : rty := cdf_aux x (enum T).
Fixpoint inverse_cdf_aux (p acc : rty) (cand : option T) (l : seq T)
: option T :=
if l is [:: y & l'] then
if acc > p then cand
else inverse_cdf_aux p (acc + d y) (Some y) l'
else cand.
Definition inverse_cdf (p : rty) : option T :=
inverse_cdf_aux p 0 None (enum T).
End cdf.
(** Product distributions *)
Lemma sum_ffunE'
(aT : finType) (rty : numDomainType) (g : aT -> rty) :
\sum_t [ffun x => g x] t =
\sum_t g t.
Proof. by apply: eq_big=> // i _; rewrite ffunE. Qed.
Lemma prod_distr_sum
(I J : finType) (rty : numDomainType) (F : I -> J -> rty) :
\prod_i (\sum_j F i j) =
\sum_(f : {ffun I -> J}) \prod_i F i (f i).
Proof. by apply: bigA_distr_bigA. Qed.
Section product.
Variable T : finType.
Variable rty : numDomainType.
Variable n : nat.
Variable f : {ffun 'I_n -> dist T rty}.
Notation type := ({ffun 'I_n -> T}).
Definition prod_pmf : {ffun type -> rty} :=
finfun (fun p : type => \prod_(i : 'I_n) f i (p i)).
Lemma prod_pmf_dist :
dist_axiom (T:=[finType of type]) (rty:=rty) prod_pmf.
Proof.
apply/andP; split.
{ rewrite /prod_pmf sum_ffunE'.
rewrite -(@prod_distr_sum _ _ rty (fun x y => f x y)).
have H: \prod_(i<n) (\sum_j (f i) j) = \prod_(i<n) 1.
{ apply: congr_big => // i _.
by rewrite dist_normalized. }
by rewrite H prodr_const expr1n. }
apply/forallP => x; rewrite /prod_pmf ffunE.
by apply: prodr_ge0 => i _; apply: dist_positive.
Qed.
Definition prod_dist : dist [finType of type] rty :=
@mkDist _ _ prod_pmf prod_pmf_dist.
End product.
Section uniform.
Variable T : finType.
Variable t0 : T.
Notation rty := rat.
Definition uniform_dist : {ffun T -> rty} :=
finfun (fun _ => 1 / #|T|%:R).
Lemma itern_addr_const n (r : rty) : iter n (+%R r) 0 = r *+ n.
Proof. by elim: n r=> // n IH r /=; rewrite IH mulrS. Qed.
Lemma ffun_lem (r : rty) :
\sum_(t : T) [ffun => r / #|T|%:R] t
= \sum_(t : T) r / #|T|%:R.
Proof. by apply/congr_big=> // i _; rewrite ffunE. Qed.
Lemma uniform_normalized : dist_axiom uniform_dist.
Proof.
rewrite /dist_axiom ffun_lem; rewrite big_const itern_addr_const.
have Hgt0: (#|T| > 0)%N.
{ move: (@enumP T); move/(_ t0)=> H; rewrite cardT.
move: (mem_enum T t0); rewrite /in_mem /=.
by case: (enum T).
}
have H: #|T| != 0%N.
{ by apply/eqP=> H; rewrite H in Hgt0.
}
apply/andP; split.
{ move: #|T| H=> n.
rewrite div1r -[_ *+ n]mulr_natl; move/eqP=> H.
apply/eqP; apply: mulfV=> //; apply/eqP=> H2; apply: H.
suff: n == 0%N; first by move/eqP=> ->.
by erewrite <-pnatr_eq0; apply/eqP; apply: H2.
}
apply/forallP=> t; rewrite /uniform_dist ffunE.
apply: divr_ge0=> //.
by apply: ler0n.
Qed.
Definition uniformDist : dist T [numDomainType of rat] := mkDist uniform_normalized.
Lemma expectedValue_uniform (f : T -> rty) :
expectedValue uniformDist f = (\sum_(t : T) (f t)) / #|T|%:R.
Proof.
rewrite /expectedValue /uniformDist /= /uniform_dist.
rewrite mulr_suml; apply/congr_big=> // t _; rewrite ffunE.
by rewrite -mulrA mul1r mulrC.
Qed.
End uniform.