package prbnmcn-stats

  1. Overview
  2. Docs
Basic statistics

Install

Dune Dependency

Authors

Maintainers

Sources

0.0.8.tar.gz
md5=9b937e69b787b3b6b2b5d4daa15a67c7
sha512=a88f6efcc3c1e5d4751dd87e58cbaa2598493d8b47954c57cd0f33bdaa252b8668dee7271389dfdfb246508e15c27763c80e5c3759a2c48312d22dbe21d0af53

doc/src/prbnmcn-stats/stats_intf.ml.html

Source file stats_intf.ml

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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
(** Type signatures. *)

(* ------------------------------------------------------------------------- *)

(** The type of (inclusive) ranges. *)
type range = { min : float; max : float }

(* ------------------------------------------------------------------------- *)

(** An ['a iterator] abstracts a finite sequence of elements. *)
type 'a iterator = ('a -> unit) -> unit

(* ------------------------------------------------------------------------- *)

(** Primitive {e representations} of distributions: empirical, generative or
    finitely supported. *)

(** ['a emp] is the type of empirical measures *)
type 'a emp = 'a array

(** ['a gen] is the type of mutable samplers of type ['a] with state of type ['s] *)
type ('s, 'a) gen = 's -> 'a

type ('a, 'r) fin_fun = ('a iterator, 'a, 'r) Vec.vec

(** [fin_mes] is the type of finitely supported measures. *)
type ('a, 'r) fin_mes = M of { total_mass : 'r; fn : ('a, 'r) fin_fun }

(** [fin_prb] is the type of finitely supported probability measures. *)
type ('a, 'r) fin_prb = P of { fn : ('a, 'r) fin_fun }

(* ------------------------------------------------------------------------- *)

(** PRNG interface, subset of {!Random.State} and [Pringo]. *)
module type Stateful_PRNG = sig
  type t

  val float : t -> float -> float

  val int : t -> int -> int

  val bool : t -> bool
end

(* ------------------------------------------------------------------------- *)

(** Type classes. *)

(** [Gen] allows to manipulate generative probabilities (ie samplers). *)
module type Gen = sig
  type state

  (** Follows the module type of a sampling-based monad *)
  include
    Basic_intf.Monad
      with type 'a t = (state, 'a) gen
       and type 'a res = (state, 'a) gen

  (** [iid gen] transforms a sampler into a sequence sampler. *)
  val iid : 'a t -> 'a Seq.t t

  (** [float bound] samples uniformly in [0; bound] *)
  val float : float -> float t

  (** [int bound] samples uniformly in [0; bound-1] *)
  val int : int -> int t

  (** [bool] samples a boolean uniformly *)
  val bool : bool t

  (** [shuffle a] samples a uniform permutation of [a]. Uses Fisher-Yates shuffle algorithm. *)
  val shuffle : 'a array -> 'a array t

  (** [uniform elts] samples an element of the [elts] array by sampling an index uniformly.

      @raise Invalid_argument if [elts] has length equal to 0 *)
  val uniform : 'a array -> 'a t

  (** [bernoulli alpha] samples [true] with probability [alpha].

      @raise Invalid_argument if [alpha] is not in the [0;1] interval. *)
  val bernoulli : float -> bool t

  (** [geometric p] samples a nonnegative integer according to the geometric law of parameter [p].

      @raise Invalid_argument if [p <= 0 || p > 1] *)
  val geometric : float -> int t

  (** [subsample ~n gen] samples one out of [n] samples from [gen]. *)
  val subsample : n:int -> 'a t -> 'a t

  (** [of_empirical emp] samples from [emp] matching exactly the empirical frequencies. *)
  val of_empirical : 'a emp -> 'a t

  (** Exponential distribution via inverse CDF.

      @raise Invalid_argument if [rate <=. 0.0]. *)
  val exponential : rate:float -> float t

  (** Gaussian distribution via Box-Muller transform.
      Returns a pair of {e independent} gaussian variates with
      prescribed mean and standard deviation.

      @raise Invalid_argument if [std <= 0.0] *)
  val box_muller : mean:float -> std:float -> (float * float) t

  (** Gaussian distribution (wrapper over box-muller transform).

      @raise Invalid_argument if [std <= 0.0] *)
  val gaussian : mean:float -> std:float -> float t

  (** Poisson distribution via inverse transform. Consider using other methods
      for large [lambda].

      @raise Invalid_argument if [lambda <= 0.0] *)
  val poisson : lambda:float -> int t

  (** Samples uniformly in the given [range].

      @raise Invalid_argument if [range] is empty. *)
  val range : range -> float t

  (** Samples uniformly in the n-dimensional axis-aligned box defined by [min] and [max].

      @raise Invalid_argument if [Array.length min != Array.length max], if the length of [min]
        or [max] is zero or if there exists an index [i] such that [min.(i) > max.(i)]. *)
  val rectangle : min:float array -> max:float array -> float array t

  (** Gamma distribution. *)
  val gamma : shape:float -> scale:float -> float t

  (** Categorical distribution. Total mass need not be one. Does not aggregate mass of equal elements.

      @raise Invalid_argument if some weights are negative or if the total mass is zero. *)
  val categorical : ('a * float) array -> 'a t

  (** [without_replacement n l] samples a subset of size [n] from [l] without replacement.

      @raise Invalid_argument if [n > List.length l]. *)
  val without_replacement : int -> 'a list -> ('a list * 'a list) t

  (** [tup2 g1 g2] samples the component of a tuple independently from [g1] and [g2]. *)
  val tup2 : 'a t -> 'b t -> ('a * 'b) t

  (** See {!tup2} *)
  val tup3 : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t

  (** See {!tup2} *)
  val tup4 : 'a t -> 'b t -> 'c t -> 'd t -> ('a * 'b * 'c * 'd) t

  (** See {!tup2} *)
  val tup5 : 'a t -> 'b t -> 'c t -> 'd t -> 'e t -> ('a * 'b * 'c * 'd * 'e) t

  (** See {!tup2} *)
  val tup6 :
    'a t ->
    'b t ->
    'c t ->
    'd t ->
    'e t ->
    'f t ->
    ('a * 'b * 'c * 'd * 'e * 'f) t

  (** [mixture weights gens] samples from the convex combination of [weights].
      [weights] need to be normalized, non-negative floats.

      @raise Invalid_argument if [mixture] is empty or if [weights] is not normalized. *)
  val mixture : float array -> (int -> 'b t) -> 'b t

  module Rational : sig
    (** Categorical distribution. Total mass need not be one. Does not aggregate mass of equal elements.

      @raise Invalid_argument if some weights are negative or if the total mass is zero. *)
    val categorical : ('a * Q.t) array -> 'a t
  end
end

module type Fin_dist = sig
  type state

  type r

  (** The type of finite probability measures with domain ['a] and range [r]. *)
  type 'a prb = ('a, r) fin_prb

  (** The type of finite measures with domain ['a] and range [r]. *)
  type 'a mes = ('a, r) fin_mes

  (** {2:finite_generic Constructing [r]-valued finitely supported functions.} *)

  (** [from_fun len f] constructs a finite function with support [0; ...; len-1].

      @raise Invalid_argument if [len < 0]
   *)
  val of_fun : int -> (int -> r) -> (int, r) fin_fun

  (** [from_array a] returns a finite function wrapping the array [a]. *)
  val of_array : r array -> (int, r) fin_fun

  (** [from_assoc h a] returns a finite function constructed from the bindings in [a].
      The finite function is backed by a hash table whose implementation is given
      through the first class module [h]. The behaviour of the function if elements
      in the support appear more than once is unspecified. *)
  val of_assoc :
    (module Hashtbl.S with type key = 'k) -> ('k * r) array -> ('k, r) fin_fun

  (** Constructing measures and probabilities *)

  (** Creates a finitely supported {e measure} from a finite function.
      A measure is not necessarily normalized.

      @raise Invalid_argument if a point as negative mass. *)
  val measure : ('a, r) fin_fun -> 'a mes

  (** Creates a finitely supported {e probability} from a finite function.
      A probability is normalized.

      @raise Invalid_argument if a point as negative mass or if the total mass does not sum up to one. *)
  val probability : ('a, r) fin_fun -> 'a prb

  (** Forgetful map from the type of finite probabilities to the type of measures. *)
  val as_measure : 'a prb -> 'a mes

  (** Normalize a measure to obtain a probability measure.

      @raise Invalid_argument if the measure has zero mass. *)
  val normalize : 't mes -> 't prb

  (** Computes the empirical measure of an array of elements. Each element
      present in the array is mapped to its count. *)
  val counts_of_empirical :
    (module Hashtbl.S with type key = 't) -> 't array -> 't mes

  (** Finitely supported uniform distribution. *)
  val uniform : 't array -> 't prb

  (** Biased coin. Raises an error if [bias] is not in [0,1]. *)
  val coin : bias:r -> bool prb

  (** Binomial distribution.
      [binomial p n] returns the probability of having
      [k] successes over [n] experiments, according to
      a biased coin [p]. *)
  val binomial : bool prb -> int -> int prb

  (** Using measures and probabilities *)

  (** Integrates a function against a finitely supported measure. *)
  val integrate : 't mes -> ('t -> r) -> r

  (** Evaluates a finitely supported probability on argument. Returns 0 if
      the argument is out of the support. *)
  val eval_prb : 't prb -> 't -> r

  (** Evaluates a finitely supported measure on argument. Returns 0 if
      the argument is out of the support. *)
  val eval_mes : 't mes -> 't -> r

  (** Iterates the given function on the support of the probability. *)
  val iter_prb : 't prb -> ('t -> r -> unit) -> unit

  (** Iterates the given function on the support of the measure. *)
  val iter_mes : 't mes -> ('t -> r -> unit) -> unit

  (** Samples from a finitely supported distribution presented as an unnormalized
      measure. This is mostly useful when sampling only once or twice from a
      distribution: consider converting to a categorical sampler when sampling
      repeatedly. Complexity: O(n) with [n] the cardinality of the support. *)
  val sample : 't mes -> (state, 't) gen

  (** Returns the total mass associated to a finitely supported measure. *)
  val total_mass : 't mes -> r

  (** Compute the mean of a finite measure supported on an [Intf.Module].*)
  val mean_generic :
    (module Basic_intf.Module with type t = 't and type R.t = r) -> 't mes -> 't

  (** Compute the mean of a finite measure supported by r. *)
  val mean : r mes -> r

  (** Compute the variance of a finite measure supported by r. *)
  val variance : r mes -> r

  (** [quantile ord mes p] computes the [p]th quantile of [mes].
      The underlying data is totally ordered by [ord].

      @raise Invalid_argument if [p < 0 || p > 1] or if the total mass of [mes] is zero *)
  val quantile :
    (module Basic_intf.Ordered with type t = 'elt) -> 'elt mes -> r -> 'elt

  (** Returns the raw data underlying a finitely supported measure. *)
  val list_of_measure : 't mes -> [> `Measure of ('t * r) list ]

  (** Returns the raw data underlying a finitely supported probability. *)
  val list_of_probability : 't prb -> [> `Probability of ('t * r) list ]

  type 'a pp := Format.formatter -> 'a -> unit

  (** Pretty print a measure, with elements sorted according to
      the order relation on the support. *)
  val pp_fin_mes : 'a pp -> 'a mes pp

  (** Pretty print a measure, with elements sorted by increasing measure.. *)
  val pp_fin_mes_by_measure : 'a pp -> 'a mes pp

  (** Fold over the union of the supports of the given measures. *)
  val fold_union :
    (module Hashtbl.S with type key = 't) ->
    ('t -> r -> r -> 'a -> 'a) ->
    't mes ->
    't mes ->
    'a ->
    'a
end

(** We use an OCamlgraph-compatible module type to describe
    undirected graphs. We assume that all graphs are undirected
    and simple. *)
module type Graph = sig
  type t

  module V : Basic_intf.Std

  type vertex = V.t

  type edge

  val nb_vertex : t -> int

  val nb_edges : t -> int

  val out_degree : t -> vertex -> int

  val mem_vertex : t -> vertex -> bool

  val mem_edge : t -> vertex -> vertex -> bool

  val succ : t -> vertex -> vertex list

  val succ_e : t -> vertex -> edge list

  val iter_vertex : (vertex -> unit) -> t -> unit

  val fold_vertex : (vertex -> 'a -> 'a) -> t -> 'a -> 'a

  val iter_edges : (vertex -> vertex -> unit) -> t -> unit

  val fold_edges : (vertex -> vertex -> 'a -> 'a) -> t -> 'a -> 'a

  val iter_succ : (vertex -> unit) -> t -> vertex -> unit

  val fold_succ : (vertex -> 'a -> 'a) -> t -> vertex -> 'a -> 'a
end
OCaml

Innovation. Community. Security.